Node.js cURL Equivalent

Kevin FOO
1 min readJan 8, 2021

--

You’ll need to install axios to run the equivalent cURL capabilities in Node.js. To do that run the following command

npm install --save axios

Lets convert the cURL command below to check public IP.

curl https://checkip.amazonaws.com

This will be the equivalent in axios

const axios = require('axios');
async function run(){
var options = {
method: 'GET',
url: 'https://checkip.amazonaws.com',
};
axios(options)
.then(function (response) {
// handle success
console.log(response.data);
})
.catch(function (error) {
// handle error
console.log(error.response.data);
})
}
run();

The above codes are for GET method. Lets take a POST method example in cURL with my favorite Pushover.

curl -s \
--form-string "token=abc123" \
--form-string "user=user123" \
--form-string "message=hello world" \
https://api.pushover.net/1/messages.json

For POST method we will be needing an additional qs package. To install it, run the following command

npm install qs

Below is the translated version of the cURL POST method

const axios = require('axios');
const qs = require('qs');

async function run(){
var data = {
'token':'abc123',
'user':'user123',
'message':'hello world',
};
var options = {
method: 'POST',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
data: qs.stringify(data),
url: 'https://api.pushover.net/1/messages.json',
};
axios(options)
.then(function (response) {
// handle success
console.log(response.data);
})
.catch(function (error) {
// handle error
console.log(error.response.data);
})
}
run();

< Back to all the stories I had written

--

--

Kevin FOO
Kevin FOO

Written by Kevin FOO

A software engineer, a rock climbing, inline skating enthusiast, a husband, a father.

No responses yet