Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!