question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change an axios instance's default headers?

See original GitHub issue

I use token in the headers for authentication. So I firstly create an axios instance to login. All the requests after login should carry the token in a header field.

Q1: Should I use instance.interceptors.request.use() to inject the token?

In my integration test, I login as different users. The instance.interceptors.request.use() accumulates the token configs in an internal array, and the login result is not always right. So I must instance.interceptors.request.eject() before each instance.interceptors.request.use() just to clear the internal array.

Q2: Is there a better way to change an instance’s default headers?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:6
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

94reactions
mzabriskiecommented, Jan 23, 2016

If you always know the value of the header:

axios.defaults.headers.common['Auth-Token'] = 'foo bar';

This will automatically add the header to every request.

20reactions
smilingpoplarcommented, Jan 23, 2016

Yes, I should update the version. https://github.com/mzabriskie/axios/blob/v0.8.1/lib/axios.js#L12

And for axios v0.9.0, set for instance:

Object.assign(instance.defaults, {headers: {authorization: 'foo bar'}});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Config Defaults | Axios Docs
Set config defaults when creating the instance const instance = axios.create({ baseURL: 'https://api.example.com' }); // Alter defaults after instance has ...
Read more >
Using Axios to set request headers - LogRocket Blog
Axios is a flexible and robust HTTP client. Learn different ways Axios can be used to set request headers for API calls.
Read more >
Attach Authorization header for all axios requests
Create instance of axios: // Default config options const defaultOptions = { baseURL: <CHANGE-TO-URL>, headers: { 'Content-Type': ...
Read more >
How To Set Request Headers Using Axios? - RapidAPI
If you want to add headers only to specific request types like GET , you can use the defaults.headers.get property. This snippet will...
Read more >
How to change header after token refresh using react axios
If the instance created after axios.create does not change, you can use interceptors.request to change the token of the header inside the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found