auth token is not assigned to headers in axios

Viewed 55

enter image description hereGetting this error:

enter image description here

     await axios
      .post("http://localhost:5000/books/addBook",
      {
        headers: {
          'Content-Type': 'application/json',
         "auth-token": localStorage.getItem('token'),
        },
       body:({
        name: String(inputs.name),
        author: String(inputs.author),
        description: String(inputs.description),
        price: Number(inputs.price),
        image: String(inputs.image),
        available: Boolean(checked),
      })
     })
      .then((res) => res.data);

this is code token has been there but not assigned to headers

1 Answers

The second parameter in post is the data that you need to send to the api and then you set the headers in the config param so in order to send the data and the headers properly you have to do this:

axios.post(url[, data[, config]])

await axios
      .post("http://localhost:5000/books/addBook",{
        name: String(inputs.name),
        author: String(inputs.author),
        description: String(inputs.description),
        price: Number(inputs.price),
        image: String(inputs.image),
        available: Boolean(checked),
      },
      {
        headers: {
          'Content-Type': 'application/json',
         "auth-token": localStorage.getItem('token'),
        },
     })
      .then((res) => res.data);
Related