0

In my react project, I make an api call to my Node backend endpoint. In this endpoint I return a response with an x-auth-token header set. The code for the response is shown below:

res.header('x-auth-token', token).send('User registered')

In react, I make an api call to this endpoint and I need to extract the x-auth-token and set the value of it in local storage. I try doing this however there is no header being sent with the response.

Here is a snippet of the code for the api call in react:

    const response = await axios.post('http://localhost:5000/api/users/', {username: username, email: email, password: password})
    console.log(response.headers.get('x-auth-token'))

In the console I get:

xhr.js:178 POST http://localhost:5000/api/users/ 400 (Bad Request)

And when I console log response.headers, in the console, I get this returned:

{content-length: "15", content-type: "text/html; charset=utf-8"}
content-length: "15"
content-type: "text/html; charset=utf-8"
__proto__: Object

Does anybody know the issue here? Thanks.

2
  • Issue is that your endpoint is not responding correctly. Commented Jul 12, 2020 at 20:07
  • right, backend issue Commented Jul 12, 2020 at 20:14

1 Answer 1

1
xhr.js:178 POST http://localhost:5000/api/users/ 400 (Bad Request)

First you have to ensure this endpoint works properly. It is not. Fix this before trying to get the auth token.

Check that your server expects a POST call, and that the body is in proper format.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.