Curl is a way you can hit a URL from your code to get an HTML response from it. In NodeJS/ExpressJS we use “request” module for this purpose. In Simple Words, if we want to access HTML content of any site we pass that URL to request( ) and it returns the HTML source code.
Curl is a popular command line tool often used for making HTTP requests. Curl supports a wide variety of other protocols, but, as a Node. js developer, you'll most likely use it to make HTTP requests to RESTful APIs.
To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option.
See the documentation for the HTTP module for a full example:
https://nodejs.org/api/http.html#http_http_request_options_callback
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)
}
})
Since looks like node-curl
is dead, I've forked it, renamed, and modified to be more curl like and to compile under Windows.
node-libcurl
Usage example:
var Curl = require( 'node-libcurl' ).Curl;
var curl = new Curl();
curl.setOpt( Curl.option.URL, 'www.google.com' );
curl.setOpt( 'FOLLOWLOCATION', true );
curl.on( 'end', function( statusCode, body, headers ) {
console.info( statusCode );
console.info( '---' );
console.info( body.length );
console.info( '---' );
console.info( headers );
console.info( '---' );
console.info( this.getInfo( Curl.info.TOTAL_TIME ) );
this.close();
});
curl.on( 'error', function( err, curlErrorCode ) {
console.error( err.message );
console.error( '---' );
console.error( curlErrorCode );
this.close();
});
curl.perform();
Perform is async, and there is no way to use it synchronous currently (and probably will never have).
It's still in alpha, but this is going to change soon, and help is appreciated.
Now it's possible to use Easy
handle directly for sync requests, example:
var Easy = require( 'node-libcurl' ).Easy,
Curl = require( 'node-libcurl' ).Curl,
url = process.argv[2] || 'http://www.google.com',
ret, ch;
ch = new Easy();
ch.setOpt( Curl.option.URL, url );
ch.setOpt( Curl.option.HEADERFUNCTION, function( buf, size, nmemb ) {
console.log( buf );
return size * nmemb;
});
ch.setOpt( Curl.option.WRITEFUNCTION, function( buf, size, nmemb ) {
console.log( arguments );
return size * nmemb;
});
// this call is sync!
ret = ch.perform();
ch.close();
console.log( ret, ret == Curl.code.CURLE_OK, Easy.strError( ret ) );
Also, the project is stable now!
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.
}
})
well if you really need a curl equivalent you can try node-curl
npm install node-curl
you will probably need to add libcurl4-gnutls-dev
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With