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.

Setting Default Headers After Create

See original GitHub issue

You can set the default headers when you call create(). Yay.

But if you want to change that after that call, you have to reach into the axiosInstance.defaults.headers object to mutate it.

Let’s provide a nicer way for this.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
skellockcommented, Jul 1, 2016

Now supported with 0.3.0 just released.

api.setHeader('Authorization', 'new stuff')
api.setHeaders({ 'X-Fun': 1, 'X-More-Fun': 69 })

More details in #20

0reactions
ryanlntncommented, Nov 16, 2018

@ningo-agilityio That would be expected behavior as any in memory object state would be reset on a browser refresh. You’ll need to store any tokens you might want to hold on to in a persistent store like local storage and restore them in a document.onreadystatechange callback. For example:

document.onreadystatechange = () => {
  if (document.readyState === 'complete') {
    const token = localStorage.getItem('authToken')
    api.setHeader('Authorization', token)
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Attach Authorization header for all axios requests
If you want, you can create a self-executable function which will set authorization header itself when the token is present in the store....
Read more >
Using Axios to set request headers - LogRocket Blog
Let's explore the different ways we can use Axios to set request headers for API calls: Passing an object argument; Creating a specific ......
Read more >
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 >
Change an axios instance's default headers? · Issue #209
So I firstly create an axios instance to login. All the requests after login should carry the token in a header field.
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 >

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