Reputation: 872
I'm trying to get data from my API with axios (with this error 400)
axios.post('http://localhost:8086/api/login',
'{"username" : "user", "password" : "pwd"}', header)
.then(response => {
console.log(response)
})
.catch(error => {
console.log(error.response)
});
with this code, it's working but I don't want to use fetch (here retrun 200)
const url = 'http://localhost:8086/api/login';
let data = '{"password": "pwd", "username": "user"}'
let fetchData = {
method: 'POST',
body: data,
headers: new Headers()
}
fetch(url, fetchData)
.then(response => {
console.log(response.body)
})
.catch((error) => {
console.log("error")
console.log(error)
});
Any idea? Thanks
Upvotes: 0
Views: 821
Reputation: 7983
From axios
docs"
axios.post(url[, data[, config]])
Where the config
is a object, see Request Config section.
So to make it working you should change the second parameter of your axios.post
method to object, instead of
'{"username" : "user", "password" : "pwd"}'
should be
{"username" : "user", "password" : "pwd"}
UPDATE
You should use it as the follow:
axios.post('localhost:8086/api/login', {"username" : "user", "password" : "pwd"}, header)
Hope it make sense.
Upvotes: 1