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?

Phil
  • 157,677
  • 23
  • 242
  • 245
  • 1
    [Authorization](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization) is a **request** header. It would be very odd for it to be present on a response – Phil Jul 27 '23 at 22:37
  • Does this answer your question? [Read Authorization header from response](https://stackoverflow.com/questions/44032464/read-authorization-header-from-response) – Phil Jul 27 '23 at 22:39
  • I'm sending a token in Authorization header from my backend, should I change it then? – coderoffuture Jul 27 '23 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 – Phil Jul 27 '23 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? – coderoffuture Jul 27 '23 at 22:46

1 Answers1

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

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

  • Ah yeah, I forgot... [Axios](https://github.com/axios/axios#response-schema)... _"All header names are lowercase and can be accessed using the bracket notation."_ – Phil Jul 27 '23 at 22:56