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

208
Visualizações
I can get correct response with Postman but body of the fetch response is empty

I send post request to my API that contains userName and password in the body. When I do that with Postman I can get a response which has a body contains a token.

enter image description here

But when I send the same request with fetch in fronted I get a responce with an empty body.

enter image description here

My feth is here:

async function login(){
    await fetch('/auth/login',
    {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(
            {
                'userName' : userName,
                'password' : password,
            }
        )
    }
).then(
    res => {
        console.log(res)
        localStorage.setItem('Authorization',JSON.stringify(res))
    }
).catch(
    err => {
        console.log("Erorororor: " + JSON.stringify(err))
    }
)
}

What is the problem?

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

0

I changed my code to

.then(res => return res.text()).then(data => console.log(data)).catch(...)
    

and now I can get the result which I want.

about 4 years ago · Juan Pablo Isaza Relatório

0

TLDR Solution

It's caused by some header settings from the server side (For example, setting Access-Control-Allow-Origin: * twice will likely cause this issue). By tuning the headers you can fix it.

Details

I met the same issue. It's NOT caused by javascript code as the browser itself doesn't get body either, but headers and cookies are got.

enter image description here enter image description here enter image description here

By expanding the timing tab you can see a warning showing the request is not finished!

enter image description here

But the postman can get the body:

enter image description here

When

The same code works in the past. It came out recently. I'm not sure what's changed as there are many things changed, for example, the browser version, the nodejs version upgraded, etc.

Fixed

By tunning the headers in the server side fixed the problem.

enter image description here

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