How to Make cURL Requests in Node.js with Examples Guide

Posted January 13, 2024
How to Make cURL Requests in Node.js with Examples

cURL (Client for URLs) is a tool library for making HTTP requests and working with various protocols. In Node.js cURL often works best when used to make requests for testing and debugging.

This way Node.js will use cURL to quickly check if an API endpoint is working as expected and inspect the responses such as access tokens, data, and total time taken to make the request.

Dive into this example guide and learn all you to need to know to execute cURL commands with Node.js APIs.

You will learn:

  • Basic example of how the cURL command looks in Node.js.
  • How to create Node.js cURL request example using Axios.
  • Using Node.js node-libcurl library to create cURL commands within Node.js.
  • How to Convert Post cURL to Node.js Request

Related: Create a HTTPS Server on NodeJS with Express, CreateServer and on Localhost

Step One: What is cURL and Why do you Need It with Node.js

Basically, A Node.js API will use cURL just like a user would. However, you customize and have more control over the request you send. This goes beyond what other API platforms such as Postman does.

A Node.js cURL request gives you control over protocols such as HTTP, HTTPS, FTP, FTPS, SCP, SFTP, LDAP, etc.

cURL is versatile and extensive.

Step Two: Basic Examples of Node.js and cURL Requests

Let’s check a few basic cURL commands that you can use with Node.js. Using cURL and Node.js to display HTTP response on a web page:

curl -o app.html <URL/app.html>

This example sends a request to a Node.js server and saves the resulting webpage as doc.html. Note that the -o flag adds the filename that the webpage will be saved as. You are not limited to .html for example, if you want a txt file:

curl -o output.txt https://example.com/file.txt

Use -X, --request to execute HTTP request methods:

curl -X POST https://api.example.com/data

If you have used Postman, you send data Payload as the request body for methods such as POST. cURL command will use the following example:

curl -X POST -d '{"key":"value"}' https://api.example.com/data

To advance this example, Postman sends raw data without any processing as JSON. Here is an example of a raw data cURL request:

curl --header "Content-Type: application/json" --data-raw '{"key":"value"}' https://api.example.com/data

Add customer Headers to the HTTP request using H or --header:

curl -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/data

cURL uses the flag -u, --user if you want to send a username and password for a basic authentication request:

curl -u username:password https://api.example.com/data

Use --form to simulate a Node.js cURL form-based file upload:

curl --form "file=@path/to/file.txt" https://api.example.com/upload

If you want to send requests with Authentication Tokens, cURL will let you send a token-based authentication request as follows:

curl --header "Authorization: Bearer YOUR_TOKEN" https://api.example.com/data

Some Node.js requests use cookies. With cURL, you only need a --cookie flag to test your Node.js APIs:

curl --cookie "name=value" https://example.com

Step Three: Practical Example of Adding cURL Requests with Node.js

In this article, I will first use cURL in Node.js node-libcurl library before diving into more examples. node-libcurl library functionalities such as form submission. I will use this example and show you how to use Node.js with cURL Requests.

Ensure you have a Node.js app ready and install the node-libcurl library:

#nPM
npm i node-libcurl --save
# yARN
yarn add node-libcurl

Step Four: Using node-libcurl with Node.js

node-libcurl uses the Curl() method to access any cURL-related elements. Create an index.js file and add the cURL and instance of the cURL class:

// Import the 'cURL' class from the 'node-libcurl' library
const { Curl } = require("node-libcurl");
// Create an instance of the 'cURL'
const curlTest = new Curl();
// Create a reference to the 'close' method instance
const terminate = curlTest.close.bind(curlTest);
// Import the 'querystring' module for working with query parameters
const querystring = require("querystring");

In this case, the querystring method gives you access to querystring API. querystring has functionality for handling URL strings.

curlTest will be the custom function you’ll create with properties related to making HTTP cURL requests as follows:

// Set the URL for the HTTP request
curlTest.setOpt(Curl.option.URL, "https://reqres.in/api/users");
// Set the HTTP method to POST
curlTest.setOpt(Curl.option.POST, true);
// Set the POSTFIELDS with query parameter using querystring module
curlTest.setOpt(
    Curl.option.POSTFIELDS,
    querystring.stringify({
        name: "thriveread",
        job: "test",
    })
);

Based on this example:

  • Node.js will use curl to send a POST request.
  • setOpt adds the URL to the request that will get sent.
  • I’m using https://reqres.in/ as the test URL.

Once Node.js executes this cURL, we expect to digest its response. I will log the following information once Node.js has subsequently sent the cURL request:

// Event listener triggered when the HTTP request is complete
curlTest.on("end", function (statusCode, data, headers) {
    // Log the status code of the response
    console.info("Status code " + statusCode);
    console.info("***");

    // Log the response data
    console.info("The response: " + data);
    console.info("***");

    // The length of the response data
    console.info("Length: " + data.length);
    console.info("***");

    // Show the total time taken for the request
    console.info("Total time taken: " + this.getInfo("TOTAL_TIME"));

    // Close the 'curlTest' instance
    this.close();
});
// Triggered in case of an error during the HTTP request
curlTest.on("error", terminate);

Once you have the details of what information is being logged, initiate the cURL request.

curlTest.perform();

Once you run this Node.js file, you should have the cURL request ready as follows:

Status code 201
Our response: {"name":"thriveread","job":"test","id":"50","createdAt":"2024-01-30T14:31:51.3352Z"}
Length: 48
Total time taken: 0.421579

Step Five: Sending cURL Requests Using Axios and Node.js

For more advanced cURL features, you might want to use the Axios or node-fetch library.

npm install axios

Use the following code to make a cURL-like request in Node.js:

const axios = require('axios');

const url = 'https://api.example.com/data';
const headers = {
  'Content-Type': 'application/json',
  // Add your authorization token if required
  'Authorization': 'Bearer YOUR_ACCESS_TOKEN', 
};

// Basic GET request
axios.get(url, { headers })
  .then(response => {
    console.log('Response:', response.data);
  })
  .catch(error => {
    console.error('Error:', error.message);
  });

// POST request with data
const postData = {
  key1: 'value1',
  key2: 'value2',
};

axios.post(url, postData, { headers })
  .then(response => {
    console.log('Response:', response.data);
  })
  .catch(error => {
    console.error('Error:', error.message);
  });

Here you have both GET and POST requests. URL, headers, and postData are customizable based on your cURL need.

Step Six: How to Convert Post cURL to Node.js Request

To convert a cURL command to a Node.js request, relevant parts of the cURL command (method, headers, data) are mapped to the corresponding options.

For example:

const axios = require('axios');

// cURL command
const curlCommand = 'curl -X POST -H "Content-Type: application/json" -d \'{"key":"value"}\' https://api.example.com/data';

// Convert cURL to Node.js request using axios
const options = {
  url: 'https://api.example.com/data',
  method: 'post',
  headers: {
    'Content-Type': 'application/json',
  },
  data: {
    key: 'value',
  },
};

axios(options)
  .then(response => {
    console.log('Response:', response.data);
  })
  .catch(error => {
    console.error('Error:', error.message);
  });

Let’s assume you have the following cURL command:

curl -v "https://us-extract.api.smartystreets.com/?auth-id=AUTH_ID&auth-token=AUTH_TOKEN" -H "content-type: application/json" --data-binary ""

In Node.js, Axios will map the cURL command options as follows:

const axios = require('axios');

const url = 'https://us-extract.api.smartystreets.com/?auth-id=AUTH_ID&auth-token=AUTH_TOKEN';
const headers = {
  'Content-Type': 'application/json',
};

// Convert cURL to Node.js request using axios
axios.post(url, null, { headers })
  .then(response => {
    console.log('Response:', response.data);
  })
  .catch(error => {
    console.error('Error:', error.message);
  });

Note that:

  • The URL is set to the endpoint of the cURL command.
  • The -H "content-type: application/json" header is added to the request based on the cURL command.
  • --data-binary "" in the cURL command sets an empty POST request body, so axios.post is set null as the data.
  • AUTH_ID and AUTH_TOKEN are the actual authentication credentials.

Conclusion

I hope you have learned:

  • Basic examples of how the cURL command looks in Node.js.
  • How to create a Node.js cURL request example using Axios and node-libcurl.
  • How to convert the cURL command to a Node.js request.
How to Make cURL Requests in Node.js with Examples Guide

Written By:

Joseph Chege