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.

overwrite/delete headers from axios.create instance

See original GitHub issue

Hi there, is the follow behavior a bug or not intend:

if i create a axios instance with a header like this

const HTTP = axios.create({
  baseURL: 'test.test',
  headers: {
    'X-Foo': 'value',
  },
});

it is no longer possible to overwrite/delete the header like

HTTP({ url: 'other', baseURL: null, headers: null })

baseUrl and url can be overwritten, headers not. why. respectively how i can?

Environment:

  • Axios Version latest

❤️ axios

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jasonsaaymancommented, Jun 6, 2019

@webdesignberlin this is due to parameters now being deep merged, though I do like the idea of being able to overwrite the whole headers object in one go. There is actually no workaround, you could set the property headers.X-Foo to null. However, once the header has been added to the instance it seems to not be possible to clear it completely. I will see if there is a way to have both deep merge as well as a way to completely clear the headers.

1reaction
chinesedfancommented, Feb 3, 2021

@webdesignberlin Do you confirm that case3 works for you? I tested with following script,

var axios = require('axios')
var adapter = function(config) {
  console.log(config.headers)
  return Promise.resolve({data: 12})
}

// set the log adapter
axios(({url:'/api', baseURL:null,headers:null, adapter}))
axios.post('/api', { content1: 'blubb1', content2: 'blubb2', }, { headers: null, adapter })
axios({ url: '/api', method: 'post', headers: null, data: { content1: 'blubb1', content2: 'blubb2', }, adapter})

Both v0.19.0 and v0.21.1 have the same output. The config merging is working as expected. The reason why post method will still have headers is due to the default transformRequest adds for object data.

{}
{ 'Content-Type': 'application/json;charset=utf-8' }
{ 'Content-Type': 'application/json;charset=utf-8' }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Axios to set request headers - LogRocket Blog
To correctly set up the headers for each request, we can create an instance of Axios using axios.create and then set a custom...
Read more >
Config Defaults | Axios Docs
Set config defaults when creating the instance const instance ... instance has been created instance.defaults.headers.common['Authorization'] = AUTH_TOKEN; ...
Read more >
Axios - update headers on exported axios.create instance
I have one api.js which exports by default an axios.create() instance:
Read more >
Setting Request Headers with Axios - Mastering JS
Learn how to set HTTP request headers on GET and POST requests with the Axios HTTP client.
Read more >
How To Set Request Headers Using Axios? - RapidAPI
HTTP headers allow clients and servers to talk to each other and pass ... Axios allows us to create new instances with custom...
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