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

176
Visualizações
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 Respostas
Responde à pergunta

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 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