Default methods Content-Type headers cant be overridden
See original GitHub issueDescribe the bug
When setting default Content-Type
for methods (e.g. patch
) always sends ContentType: application/json;charset=utf-8
To Reproduce
const api = axios.create({
baseURL: '/api',
headers: {
common: {
Accept: 'application/json'
},
patch: {
'Content-Type': 'application/merge-patch+json'
}
}
})
api.patch('/user/1', { foo: 'bar' })
Expected behavior
Should send Content-Type: application/merge-patch+json
header.
Environment:
- Axios Version 0.19.0
- OS: macOS 10.15.1
- Browser Chrome, Safari
- Browser Version 78.0.3904.108
Additional context/Screenshots
I think problem is here – root headers from config.headers
always overrides headers for specific methods. In example above config.headers['Content-Type']
not set, so axios set it to application/json;charset=utf-8
under the hood and config.headers['Content-Type']
overrides config.headers.patch['Content-Type']
.
I think methods headers should have higher priority and should be passed to utils.merge
function in last argument
Issue Analytics
- State:
- Created 4 years ago
- Reactions:13
- Comments:12 (1 by maintainers)
Top Results From Across the Web
Impossible to override Content-Type header? - Google Groups
The only thing I find confusing is that the Set(key, value string) doesn't override the Content-Type header if it already exists. The API...
Read more >Why would spring overwrite Content-Type in header?
There is a default spring configuration, based on 3 strategies which select the content type to be returned. The configuration could be modified ......
Read more >How to add Content-Type for income request by conditions ...
I have a scenario that adding 'application/json' as default Content-Type for incoming request if cannot find Content-Type from request in MPGW, ...
Read more >RFC 7231: Hypertext Transfer Protocol (HTTP/1.1)
This document defines the semantics of HTTP/1.1 messages, as expressed by request methods, request header fields, response status codes, and response header ......
Read more >Can We Override Default Method in Java? - GeeksforGeeks
It is a type of non-abstract method. This method is capable of adding backward capability so that the old interface can grasp 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
@WalderlanSena your example uses explicit headers settings for the request. This issue is about defaults provided to
axios.create
.Workaround
Apply a custom
transformRequest
suitable for your usecase instead.Result: