Node.js - Request to an API using Http Client
To Create an Http client to consume the APIs in Node, we have a very popular third-party package i.e Request. But, Request is now fully deprecated, you can check more details at npmjs.com/package/request
As an alternative, we are going to use the postman-request npm package. This package has bug fixes that the Request module does not have.
Get users from the API
To Create an Http client to consume the APIs in Node, we have a very popular third-party package i.e Request. But, Request is now fully deprecated, you can check more details at npmjs.com/package/request
As an alternative, we are going to use the postman-request npm package. This package has bug fixes that the Request module does not have.
Get users from the API
const request = require("postman-request");
const getusers = (Mycallback) => {
request("https://reqres.in/api/users?page=2", function ( error,response,body ) {
if (error) {
return console.log(error);
}
if(body){
Mycallback(body)
}
});
};
getusers((data) => {
console.log(data);
});
Save Users to the Json File
const request = require('postman-request');
const fs = require('fs')
const getusers = (Mycallback) => {
request("https://reqres.in/api/users?page=2", function ( error,response,body ) {
if (error) {
return console.log(error);
}
if(body){
Mycallback(body)
}
}).pipe(fs.createWriteStream('users.json'))
};
getusers((data) => {
console.log(data);
});
0 Comments