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.

Default methods Content-Type headers cant be overridden

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Reactions:13
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
pelepelincommented, Dec 19, 2020

@WalderlanSena your example uses explicit headers settings for the request. This issue is about defaults provided to axios.create.

2reactions
pelepelincommented, Jan 15, 2020

Workaround

Apply a custom transformRequest suitable for your usecase instead.

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:

application/json
Read more comments on GitHub >

github_iconTop 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 >

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