Axios Custom Header

Posted on  by admin
Modified1 month ago.

How can I send an authentication header with a token via axios.js?I have tried a few things without success, for example: . Gives me this error: . I have managed to get it work by setting global default, but I'm guessing this is not the best idea for a single request: .

Cole's answer helped me find the problem.

I am using django-cors-headers middleware which already handles authorization header by default. But I was able to understand the error message and fixed an error in my axios request code, which should look like this .

4747 bronze badges.
relentless-coderrelentless-coder
1,400

If any of the headers you want to send were not listed in either the spec's list of whitelisted headers or the server's preflight response, then the browser will refuse to send your request.

In your case, you're trying to send an Authorization header, which is not considered one of the universally safe to send headers.

The browser then sends a preflight request to ask the server whether it should send that header.

The server is either sending an empty Access-Control-Allow-Headers header (which is considered to mean "don't allow any extra headers") or it's sending a header which doesn't include Authorization in its list of allowed headers.

Are you adding new JS libraries to improve performance or build new features? What if they’re doing the opposite?