When My Vuejs code try to post JSON, backend returned 400 error.
I found clue for that by chrome network capture.
My Axios code sended JSON with data-binary format.
curl 'http://localhost:5000/api/auth/login' \
-H 'Content-Type: application/json;charset=UTF-8' \
-H 'Accept: application/json' \
--data-binary '{"username":"admin","password":"ehllow"}'
But I don't know how it works like it.
My expecting request is
curl ... -D '{"username":"admin","password":"ehllow"}'`
not --data-binary
Here is my javascript code.
axios({
method: 'post',
url: '/api/auth/login',
data: {
"username": "admin",
"password": "ehllow"
},
config: {
headers: {
'Content-Type': 'application/json'
}
}
})
.then(function (response) {
//handle success
console.log(response);
})
.catch(function (response) {
//handle error
console.log(response);
});
How can I change my axios code to send json to normal Data(-D
)
I had the same question, answer was here: axios post request to send form data
In short:
let data = new FormData();
data.set("username", "admin");
data.set("password", "ehllow");
axios({
method: 'post',
url: '/api/auth/login',
data: data,
config: {
headers: {
'Content-Type': 'application/json'
}
}
})
Threw me for a loop, as binary data is read differently than form data.