Axios Equivalent Of Curl
What is the (nodeJS) axios equivalent of: curl --location --request POST '' \ --header 'API-Authorization: ' \ --form 'quantity=1' \ --form 'offset=0' Tried:
Solution 1:
The NodeJS equivalent of your CURL request code should be something like this:
const axios = require('axios');
const qs = require('qs');
let data = qs.stringify({ 'quantity': '1', 'offset': '0'});
let config = {
method: 'post',
url: 'https://someurl.com',
headers: {
'API-Authorization': '<key>',
'Content-Type': 'application/x-www-form-urlencoded'
},
data : data
};
axios(config)
.then((response) => {
console.log(JSON.stringify(response.data));
})
.catch((error) => {
console.log(error);
});
What you need for this is the qs
(QueryString) package, simply install it by npm install qs
. I got this output simply by generating the request in Postman.
Post a Comment for "Axios Equivalent Of Curl"