Reputation: 108500
What is the (nodeJS) axios equivalent of:
curl --location --request POST '<URL>' \
--header 'API-Authorization: <KEY>' \
--form 'quantity=1' \
--form 'offset=0'
Tried:
const FormData = require('form-data')
const data = new FormData()
data.append('quantity', '1')
data.append('offset', '0')
axios({
url: <URL>,
method: 'POST',
data,
headers: {
'API-Authorization': <KEY>
}
})
But the server gives 500
Upvotes: 0
Views: 2164
Reputation: 4925
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.
Upvotes: 2