DEV Community

Suhail Akhtar
Suhail Akhtar

Posted on • Originally published at suhail.hashnode.dev on

Node.js Tutorial: How to Easily Convert Axios Requests to cURL Commands with axios-curlirize in Node.js and the Browser

Axios is a popular JavaScript library used for making HTTP requests from a web page or Node.js application. When developing APIs, it can be helpful to convert an Axios request to a cURL command for debugging or testing purposes. Fortunately, the axios-curlirize npm package makes it easy to do just that. In this tutorial, we'll show you how to use axios-curlirize to convert Axios requests to cURL commands.

Prerequisites

Before we get started, make sure you have the following installed:

  • Node.js (version 12 or higher)

  • NPM (Node Package Manager)

Installing axios-curlirize

To use axios-curlirize, you need to install it using NPM. Open your terminal and run the following command:

npm install axios-curlirize
Enter fullscreen mode Exit fullscreen mode

Using axios-curlirize

Once you have axios-curlirize installed, you can start using it to convert Axios requests to cURL commands. Here's an example:

const axios = require('axios');const axiosCurlirize = require('axios-curlirize');axiosCurlirize(axios);axios.get('https://jsonplaceholder.typicode.com/posts/1') .then(response => { console.log(response.data); }) .catch(error => { console.error(error); });
Enter fullscreen mode Exit fullscreen mode

In this example, we import axios and axios-curlirize. We then call axiosCurlirize with the axios instance as an argument to enable cURL logging for Axios requests. After that, we make a GET request to https://jsonplaceholder.typicode.com/posts/1 using Axios.

When you run this code, you'll see the cURL command for the Axios request logged to the console, along with the response data:

$ node app.js

$ curl 'https://jsonplaceholder.typicode.com/posts/1' -H 'User-Agent: axios/0.21.1' -H 'Accept: application/json, text/plain, */*' -H 'Host: jsonplaceholder.typicode.com' -H 'Connection: keep-alive'{ userId: 1, id: 1, title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', body: 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto'}
Enter fullscreen mode Exit fullscreen mode

And that's it! You can now use axios-curlirize to convert your Axios requests to cURL commands for debugging or testing purposes. It's a simple and effective tool to have in your developer toolkit.

Top comments (0)