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

169
Vistas
Empty body POST request express

I trying to setup an express server, however all of my POST request have an empty body once it reach the server. I've tried to use body-parser or express.json() but the result didn't change. A console.log(req.body) only give me a empty object {}

The data is sent without problems, as the devs tools networks shows the json object that I'm trying to send. The code is still fairly simple in both the express api and the react website so I'm not sure what I'm doing wrong here:

Express api :

const bodyParser = require('body-parser');
var express = require('express');
var router = express.Router();
var app = express();

app.use(bodyParser.urlencoded({extended : false}));
app.use(bodyParser.json())
//app.use(express.json())

router.use((req,res,next)=>{
    res.header('Access-Control-Allow-Origin','*')
    res.header("Access-Control-Allow-Methods", "GET, POST");
    next()
})

router.post("/",function (req,res,next){
    console.log(req.body)
    res.send("blabla")
})

module.exports = router;

React :

async createChat(){
        var data = {
            method:'POST',
            body:JSON.stringify()
        }
        console.log(data)
        await fetch('http://localhost:5000/create_chat',{
            method: 'POST',
            body: JSON.stringify({
                chat_name:this.state.chatName,
                password : {
                    enabled: this.state.passwordYesNo,
                    password : this.state.password
                },
                public:this.state.publicYesNo,    
            })
        })
        .then((res)=>res.text())
        .then((res)=>console.log(res))
   }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The issue is caused by the missing headers parameter in the fetch call. You can update the fetch call like this and try.

    await fetch('http://localhost:5000/create_chat',{
        method: 'POST',
        headers: new Headers({'content-type': 'application/json'}),
        body: JSON.stringify({
            chat_name:this.state.chatName,
            password : {
                enabled: this.state.passwordYesNo,
                password : this.state.password
            },
            public:this.state.publicYesNo,    
        })
    })
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