1
const sendRequest = async () => { try { const response = await axios.post("http://localhost:8080/auth/login", { username: username, password: password, }); console.log(response.data); const authorizationHeader = response.headers["Authorization"]; if (authorizationHeader) { localStorage.setItem("token", authorizationHeader); console.log("Token stored in localStorage."); } else { console.log("No Authorization header received."); } } catch (error) { console.log("Error:", error); } }; 

this is my function above but even though I see the header I want to get "Authorization" on both network on chrome browser and postman I'm not able to get it. What am I doing wrong?

5
  • 1
    Authorization is a request header. It would be very odd for it to be present on a response Commented Jul 27, 2023 at 22:37
  • Does this answer your question? Read Authorization header from response Commented Jul 27, 2023 at 22:39
  • I'm sending a token in Authorization header from my backend, should I change it then? Commented Jul 27, 2023 at 22:40
  • Pragmatically, you can send any header you want in the response but IMO, I would rename it to something like x-auth-token. You'll still need the appropriate access-control-expose-headers though (see the duplicate link). Even better would be to send it in a cookie Commented Jul 27, 2023 at 22:42
  • my backend is spring boot and security is really confusing me and my time is limited so is it worth changing atm? Commented Jul 27, 2023 at 22:46

1 Answer 1

1
const authorizationHeader = response.headers['authorization']; 

changing it the first letter of header name to lower case fixed my problem

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

1 Comment

Ah yeah, I forgot... Axios... "All header names are lowercase and can be accessed using the bracket notation."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.