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

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

curl -X POST

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"}'

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"}'

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

curl -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_TOKEN"

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

curl -u username:password

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

curl --form "file=@path/to/file.txt"

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"

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

curl --cookie "name=value"

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 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, "");
// Set the HTTP method to POST
curlTest.setOpt(Curl.option.POST, true);
// Set the POSTFIELDS with query parameter using querystring module
        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 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"Status code " + statusCode);"***");

    // Log the response data"The response: " + data);"***");

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

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

    // Close the 'curlTest' instance
// 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.


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 = '';
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 => {
  .catch(error => {
    console.error('Error:', error.message);

// POST request with data
const postData = {
  key1: 'value1',
  key2: 'value2',
};, postData, { headers })
  .then(response => {
  .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"}\'';

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

  .then(response => {
  .catch(error => {
    console.error('Error:', error.message);

Let’s assume you have the following cURL command:

curl -v "" -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 = '';
const headers = {
  'Content-Type': 'application/json',

// Convert cURL to Node.js request using axios, null, { headers })
  .then(response => {
  .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 is set null as the data.
  • AUTH_ID and AUTH_TOKEN are the actual authentication credentials.


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