-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default methods Content-Type headers cant be overridden #2623
Comments
@Ashot-KR I've got the same problem. As a workaround, i'm using an interceptor.
It is working for me. Hope this help. EDIT: But axios provide their own defaults for post
|
I've found a similar issue, i think it's the same, but my analysis is different. Issue When an object is passed in Reproduce require('axios').create({
headers: {
post: {
'Content-Type': 'application/json'
}
}
}).request({
url: 'http://httpbin.org/anything',
method: 'post',
data: {}
}).then(response => console.log(response.data.headers['Content-Type'])); Result:
Reason
axios/lib/core/dispatchRequest.js Lines 30 to 37 in 351cf29
However, default Lines 50 to 51 in 351cf29
So, default by method |
Workaround Apply a custom require('axios').create({
transformRequest: (data, headers) => {
if (data !== undefined) {
headers['Content-Type'] = 'application/json';
return JSON.stringify(data)
} else {
return data;
}
}
}).request({
url: 'http://httpbin.org/anything',
method: 'post',
data: {}
}).then(response => console.log(response.data.headers['Content-Type'])); Result:
|
Workaround 2 Serialize data explicitly. require('axios').create({
headers: {
post: {
'Content-Type': 'application/json'
}
},
}).request({
url: 'http://httpbin.org/anything',
method: 'post',
data: JSON.stringify({})
}).then(response => console.log(response.data.headers['Content-Type'])); Result:
|
+1 |
A solution that is working for me is to set the Content-Type header in the headers property instead of common:
I think the issue is in dispatchRequest.js: // Transform request data
config.data = transformData(
config.data,
config.headers,
config.transformRequest
);
// Flatten headers
config.headers = utils.merge(
config.headers.common || {},
config.headers[config.method] || {},
config.headers
); The flatten headers should be done BEFORE the transform request data. Otherwise, axios set the Content-Type to application/json;utf-8 if the data sent is an object and after the merge cannot override it since it is set. Also, the function setContentTypeIfUnset should have a third parameter to look into the current method of the request or have common as default value if not provided: function setContentTypeIfUnset(headers, value) {
if (!utils.isUndefined(headers) && utils.isUndefined(headers['Content-Type'])) {
headers['Content-Type'] = value;
}
} Currently, it looks for headers['Content-Type'] and not headers.common['Content-Type']. |
Just change the format of the sent data to JSON Use stringify |
@WalderlanSena your example uses explicit headers settings for the request. This issue is about defaults provided to |
I believe this should be amended in the latest version of Axios, please try it out and if the issue still exists, please open a new pull request. |
@jasonsaayman which version was this fixed in? I'm seeing it in 0.21.1 (the version at the time of your last comment) which is used by the nuxt-community axios-module and would like to know if upgrading that and nuxt auth to use 0.24 would allow me to set a default Content-Type header for my patch calls. I don't see this issue listed in the change log, or an entry that appears to match. I looked in the tests and I'm not seeing test coverage for this case. Did I miss it? |
In my case I had to do this: this.instance.defaults.transformRequest = [(data, headers) => {
if (data && headers) {
if (headers["content-type"] === "application/json" || headers["Content-Type"] === "application/json") {
headers["Content-Type"] = "application/json"
return JSON.stringify(snakecaseKeys(data, { deep: true }))
}
}
}] I think there is an inconsistency going on, because response sees is as "content-type" and request as "Content-Type". If you try in your instance creation this.instance = axios.create({
baseURL,
headers: { 'content-type': 'application/json' }
}); Because of the merge process, then axios will send content-type: "application/json, application/json". Feel this is error prone. |
Describe the bug
When setting default
Content-Type
for methods (e.g.patch
) always sendsContentType: application/json;charset=utf-8
To Reproduce
Expected behavior
Should send
Content-Type: application/merge-patch+json
header.Environment:
Additional context/Screenshots
I think problem is here – root headers from
config.headers
always overrides headers for specific methods. In example aboveconfig.headers['Content-Type']
not set, so axios set it toapplication/json;charset=utf-8
under the hood andconfig.headers['Content-Type']
overridesconfig.headers.patch['Content-Type']
.I think methods headers should have higher priority and should be passed to
utils.merge
function in last argumentThe text was updated successfully, but these errors were encountered: