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.

Wrong content-type used [serious bug]

See original GitHub issue

I’ve manually set the content type to application/json before the axios.post() call.

Yet, when the request is made, it’s picking application/x-www-form-urlencoded huh? So I’ve added a transformRequest function and printed out the headers there:

Screen Shot 2020-01-10 at 16 43 26

You can see the JSON object for header, it’s has nested content types inside, for each method. It ignored the content type I’ve manually set.

If I add these code lines then it will work

// unset silly defaults
axios.defaults.headers.post = null
axios.defaults.headers.put = null

but that’s kinda silly and smells. A bug? I’ve read documentation carefully still am in the dark.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
avindracommented, Feb 19, 2020

Update: I figured out when it happens.

So if the code tries to POST but the body isn’t set, axios resolves to that broken code path that is using the wrong header (picking from axios default rather than the configured options on the instance).

1reaction
avindracommented, Feb 19, 2020

I’m having this issue and I’m not using transformRequest. My axiosInstance has been created as such:

  const instance = axios.create({
    baseURL: '/api/whatever',
    common: {
      'Content-Type': 'application/json',
      Accept: 'application/json',
    },
  });

Since common is set, I would expect the default to be application/json, but I’m seeing the issue @binarykitchen originally reported, where on some POST calls, the Content-Type is erroneously set to application/x-www-form-urlencoded.

Which POST calls is completely unknown to me. It appears to be random, but this is just code so I’m assuming there is a reproducible error condition.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTTP status code for unaccepted Content-Type in request
For certain resources, my RESTful server only accepts PUT and POST requests with JSON objects as the content body, thus requiring a Content-Type...
Read more >
Error “The content type is in use” explained
Recently I have developed a tool to manage content types from a content type hub. Think about renaming content types or changing the...
Read more >
How to resolve parse error when HTTP content-type header is ...
The HTTP content-type header is missing in the request or response message. Resolving The Problem. In order to resolve this issue, one can...
Read more >
6800 - Chrome doesn't set Content-Type for file upload when ...
I just reproduced this bug on Chrome 7.0.517.41 when trying to upload the attached files. Note that the seq file is plain text,...
Read more >
RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1
It is a generic, stateless, protocol which can be used for many tasks beyond its ... through extension of its request methods, error...
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