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.

Content-Type header removed on a POST with no content.

See original GitHub issue

Summary

We have a bit of an edge case but I believe that it is a bug. We have a POST with an empty payload. When the post is executed the Content-Type header is removed and is causing a CORS issue.

Context

Let me explain what does work. Outside of a browser, our API does not care if you send or omit the Content-Type header as there is no payload to inspect. Within a browser, it appears that you must pass the content-type header to ensure that CORS is done correctly.

We create our client with the following code

const webserviceClient = axios.create({
  baseURL: REACT_APP_WS_ENDPOINT,
  headers: {
    'Content-Type': 'application/json;charset=utf-8',
  },
})

But when we execute a post without body content (data) the axios client appears to remove that header and we get a 403 back from the server and a cors error.

Even using fetch within the browser I can mimic this issue by removing the content-type header. image

Here is the code that fails.

export const testDeliveryPreferences = () => {
  return webserviceClient.post(`/v1/user/delivery-preferences/ftp-test`)
}

This also fails

export const testDeliveryPreferences = () => {
  return webserviceClient.post(`/v1/user/delivery-preferences/ftp-test`, undefined)
}

And these two both work

export const testDeliveryPreferences = () => {
  return webserviceClient.post(`/v1/user/delivery-preferences/ftp-test`, null)
}

export const testDeliveryPreferences = () => {
  return webserviceClient.post(`/v1/user/delivery-preferences/ftp-test`, {})
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
maxrahdercommented, Mar 4, 2019

I have this same issue. My backend is some goofy Domino service I can’t control. I have to send form data and set the content header to what Domino expects, yet Axios seems to think it knows better and overwrites my setting. This should be up to the Axios user, not Axios – there are weird non-standard backends out there, and I should be allowed to comply with them, and not with how Axios wishes they were implemented.

2reactions
nGolinecommented, Jul 1, 2021

~Does anyone have a workaround for this while we wait for the fix?~

I was able to send the request without the Content-Type header by using this code:

const response = await axios.post(
  url,
  null,
  {
    headers: { ... },
    transformRequest: (data, headers) => {
      delete headers.post['Content-Type'];
    },
  },
);

I was trying to call an API and since I’m not sending any data they’re not expecting Content-Type to be on the header and the call fails.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Post data with curl with no Content-Type header
Remove an internal header by giving a replacement without content on the right side of the colon, as in: -H "Host:" .
Read more >
Content-Type - HTTP - MDN Web Docs - Mozilla
In requests, (such as POST or PUT ), the client tells the server what type of data is actually sent. Header type, Representation...
Read more >
Solved: DELETE Method Without Content-Type Header?
Solved: Is it possible to create a DELETE request (either HTTP or REST) without having to supply a Media Type from the dropdown?...
Read more >
Post Request - Missing Content Type | Imperva
Post Request - Missing Content Type Summary This alert occurs when HTTP Post requests are received which have a body but the content-type...
Read more >
RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1
RFC 2616 HTTP/1.1 June 1999 content-type field if they support that ... PUT or POST request that does not include an Expect request-header...
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