Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

204
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda