Goal:
Use Axios to send a post from client to backend by using React TS
Problem:
Axios code cannot send data to the backend while fetch code can make it.
The data, in relation, to axios will be id = 0 and firstname = null at backend
It works well when I use fetch code and I can see it at backend.
What part am I missing in order to send data as a post to backend in relation to axios?
Info:
*It doesn't work to use 'application/json'.
*It works when I use post man tool.
*The difference is Content-length and I tried to change number at axios post but it doesn't work.
Thank you!
const params = {
id: 1,
firstName: "sdf"
}
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'accept': '*/*'
},
accept: { }
};
axios.post(AddClient, params, config)
.then(response => {
})
.catch(error => {
console.log(error.response)}
);
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/x-www-form-urlencoded");
var urlencoded = new URLSearchParams();
urlencoded.append("id", "1");
urlencoded.append("firstName", "sdf");
let requestOptions = {
method: 'POST',
headers: myHeaders,
body: urlencoded
};
fetch("http://localhost:1122/api/v1/Test/AddClient", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));