axios - 为什么我不能覆盖请求标头


const config = {
headers: {
'Content-Type': 'application/json',
accept: 'application/json',
},
baseURL: 'https://my-site.com',
};
const axiosInstance = axios.create({config});
// override here 
const result = await axiosInstance.get('url', {
headers: { Authorization: 'Bearer ' + accessToken, Accept: '*/*' },
params: {}
})

在请求头中,Accept仍然显示"application/json"而不是*/*,而Authorization显示正确的值。

如何覆盖axiosInstance.get中的标头?

更新我的问题类似于https://github.com/axios/axios/issues/1819

这是因为您设置了accept标头而不是accept标头。在第一种情况下,该值会附加到默认值。在第二种情况下,将替换该值。

演示链接:https://codesandbox.io/s/mystifying-einstein-0ye2gq

最新更新