1

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

5
  • Does this answer your question? Passing headers with axios POST request Commented Aug 14, 2022 at 16:46
  • Can we have the Request header in the network tab? Commented Aug 14, 2022 at 16:50
  • 1
    Is your backend accepting auth-token header? Commented Aug 14, 2022 at 17:09
  • @underscore yess Commented Aug 15, 2022 at 7:40
  • no whenever i send request then the headers are gone in data not as headers Commented Aug 15, 2022 at 7:50

1 Answer 1

2

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); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.