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

314
Vistas
JSON String not getting to the server (should be easy to solve if you know express js)

I am using express js but for some reason, the server is logging an empty object {} instead of the actual JSON string that I sent. I have worked with so many other technologies like flask, this makes no sense.

Code:

function upload () {
    fetch("http://localhost:8080/chat", {
        method: "POST",
        body: JSON.stringify({
            name: "Deska",
            email: "deska@gmail.com",
            phone: "342234553"
        })
    }).then(result => {
        // do something with the result
        console.log("Completed with result:", result);
    }).catch(err => {
        // if any error occured, then catch it here
        console.error(err);
    });
}

app.post('/chat', function(req, res) {

let test = req.body;
console.log(test);
}

On the "upload" function I do not get the anything logged, and in the server, I get the an empty object {} I mentioned.

If you are to know my issue, I would appreciate help.

Thank you.

UPDATE:

Issue should be in the prontend, as sending the post request with postman works.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I think the error could be happening because you are missing the Content-Type header. You could try this:

function upload () {
    fetch("http://localhost:8080/chat", {
        headers: {
            'Content-Type': 'application/json',
        }, 
        method: "POST",
        body: JSON.stringify({
            name: "Deska",
            email: "deska@gmail.com",
            phone: "342234553"
        })
    }).then(result => {
        // do something with the result
        console.log("Completed with result:", result);
    }).catch(err => {
        // if any error occured, then catch it here
        console.error(err);
    });
}

You should also make sure that in your server you are using the express.json middleware, this way:

app.use(express.json());
over 4 years ago · Santiago Trujillo 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