0

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!

enter image description here

enter image description here

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));
HelloWorld1
  • 12,940
  • 25
  • 73
  • 128
  • 1
    Does this answer your question? [axios post request to send form data](https://stackoverflow.com/questions/47630163/axios-post-request-to-send-form-data) – Khan Asfi Reza Jan 24 '22 at 17:23

0 Answers0