Change an axios instance's default headers?
See original GitHub issueI 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:
- Created 8 years ago
- Reactions:6
- Comments:14 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you always know the value of the header:
This will automatically add the header to every request.
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: