Fetch API - Content-Type is sent as text/plain when it's set to application/json

Viewed 11340

In my React app when I submit a form to create a new user I create a request to the server using fetch.

The code is this one:

fetch('http://0.0.0.0:3000/users', {
  method: 'POST',
  mode: 'no-cors',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(data)
}).then(response => console.log(response))

When I check the entry in the Network tab under the Developer Tools I see that the Content-Type is text/plain but I do not understand why if I set it as application/json.

I have tried using the option Edit and send in the request and replace text/plain to application/json but after sending it it is sent as text/plain.

1 Answers

The mode:"no-cors" option is the culprit.

Remove that option and it should work

To fix it pass a new headers object

headers: new Headers({'content-type': 'application/json'})
Related