I\'m looking to use information from an HTTP request using Node.js (i.e. call a remote web service and echo the response to the client).
In PHP I would have used cUR
Request npm module Request node moulde is good to use, it have options settings for get/post request plus it is widely used in production environment too.
The http module that you use to run servers is also used to make remote requests.
Here's the example in their docs:
var http = require("http");
var options = {
host: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
you can easily use request module:
https://www.npmjs.com/package/request
Sample code:
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Show the HTML for the Google homepage.
}
else {
console.log("Error "+response.statusCode)
}
})
You can use request npm module . Super simple to use. Request is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default.
var request = require('request');
request('http://www.google.com', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
See the documentation for the HTTP module for a full example:
https://nodejs.org/api/http.html#http_http_request_options_callback
For new projects please refrain from using request, since now the project is in maitainance mode, and will eventually be deprecated
https://github.com/request/request/issues/3142
Instead i would recommend Axios, the library is in line with Node latest standards, and there are some available plugins to enhance it, enabling mock server responses, automatic retries and other features.
https://github.com/axios/axios
const axios = require('axios');
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
Or using async / await:
try{
const response = await axios.get('/user?ID=12345');
console.log(response)
} catch(axiosErr){
console.log(axiosErr)
}
I usually use REQUEST, its a simplified but powerful HTTP client for Node.js
https://github.com/request/request
Its on NPM
npm install request
Here is a usage sample:
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Show the HTML for the Google homepage.
}
})