ShintoTuna
ShintoTuna

Reputation: 3787

Wrong Content-Type being substituted for fetch http request

I have this code on client:

return fetch('http://localhost:8080/api/authenticate', {
    method: 'POST',
    mode: 'no-cors',
    headers: {
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        username: 'admin',
        password: 'admin',
    }),
});

When i send this request for some reason Content-Type is substituted to text/plain;charset=UTF-8. This makes my server side fail the request as it only accepts application/json requests. What am i doing wrong here? I am using Chrome 51 and here is my request:

enter image description here

EDIT: When I remove JSON.strigify() Content-Type and Request payload are also being omitted. Here is an example:

Result without JSON.stringify()

Upvotes: 0

Views: 3232

Answers (1)

Quentin
Quentin

Reputation: 943214

You have set mode: 'no-cors', so you cannot set 'Content-Type' to 'application/json'. It isn't one of the safe values for Content-Type.

Upvotes: 5

Related Questions