mots
mots

Reputation: 315

Get data with post using axios in node.js

How can I get data from an api that uses post to get data? Here is my code but the api does not return anything when I log the data.

Upvotes: 0

Views: 2952

Answers (2)

zolamk
zolamk

Reputation: 6377

i think the data you are trying to pass as the requests body is meant to be url encoded form data so, try this

axios.post('https://example.com/api?function=getSomething')
.then(function(response) {
      console.log(response.data);
    });

Upvotes: 1

Cr.
Cr.

Reputation: 916

Change Content-Type which the server support. Qs is querystring library.

axios.post('https://example.com/api', Qs.stringify({
    'function': 'getEvents'
}), {
    withCredentials: true,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(function(response) {
    console.log(response.data);
}, function(error) {
    console.log(error);
})

Code in jsfiddle, but it also get an error by Access-Control-Allow-Origin, you may proxy yourself or change the server origin access allow.

Upvotes: 1

Related Questions