const AUTH = () => {
const params = JSON.stringify({
user: {
user_type: 'customer',
mobile: '8349222680',
password: '12345',
},
});
const url = 'https://salon-host-dev.herokuapp.com/api/v1/logins/';
axios
.post(url, {
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
data: 'data=' JSON.stringify(params),
}),
})
.then(response => {
console.log('--->', response.status);
});
};
I am trying to get response from url but unable to that if Params passed in raw body json in postman then i am geeting expected response but not in following code please help
CodePudding user response:
The syntax for axios post request is below
axios.post(url, params,
{headers: {'Content-Type':'application/json'}})
.then()
The header should be the 3rd argument to the POST method.
CodePudding user response:
axios
(and axios.post
for that matter) can be called like axios.post(config)
axios.post(url[, config])
axios.post(url[, data [, config]])
.... so you've chosen the second pattern,
the data to send is in data
property, NOT the body
property -
also you DONT need to send JSON, you can send the object
So given
const params = {
user: {
user_type: 'customer',
mobile: '8349222680',
password: '12345',
},
};
const url = 'https://salon-host-dev.herokuapp.com/api/v1/logins/';
You can
axios.post({
url: url,
headers: {
'content-type': 'application/json',
},
data: params,
})
or
axios.post(url, {
headers: {
'content-type': 'application/json',
},
data: params,
})
or
axios.post(url, params, {
headers: {
'content-type': 'application/json',
}
})
Additionally, if you rename params to data
const data = {
user: {
user_type: 'customer',
mobile: '8349222680',
password: '12345',
},
};
and create a headers variable
const headers = {
'content-type': 'application/json',
};
you can do any of the following
axios.post(url, data, { headers });
axios.post(url, {data, headers});
axios.post({url, data, headers});