Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

313
Visualizações
Fetch: Content-Type: application/json not changing in post method. Mode set to cors

I am new to using the fetch function and can't figure out why this fetch function wont change the headers. Specifically I can't seem to change the content-type to json making the API return a 415. I have tried with mode set to both 'cors' and 'no-cors' and neither seems to make the difference.

const fetchOptions: RequestInit = {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}
await fetch("https://localhost:44375/api/users/login", fetchOptions)
.then(response => {
    console.log(response);
})

Here is a link to a picture of my Chrome console:

Here is a link to a picture of my Chrome console

As you can see, it is content-type plain text while also being in cors mode.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The issue here was server side and not client side. My web api was not allowing the Content-Type header to be changed due to a pre-flight OPTIONS request from the browser. This was fixed by properly setting up the cors policy on the server side.

In my case I am writing a C# api and it was as simple as adding WithHeaders to my policy:

builder.Services.AddCors(options => options.AddPolicy(name: "DevOrigins",
    builder =>
    {
        builder.WithOrigins("http://localhost:3000");
        builder.WithHeaders("Content-Type");
    })
);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda