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

131
Views
Issue w/ JSON for bodyParser

I'm sending data in my React component via the Fetch API, and returning the result as JSON. When POSTED on my Express server, I use the jsonParser method from bodyParser to parse through the data, but insead I'm only getting back a empty object. I do not understand what's the issue with jsonParser, because if I use textParser, my data get sent fine.

Edit: When printing out the request (req) on the server, it is showing that nothing was received in the body. This only happens with jsonParser though, and not textParser.

Fetch:

fetch('./test',{
  method: 'POST',
  body: ["{'name':'Justin'}"]
})
.then((result) => {
      return result.json();
    })
.then((response) => {
      console.log(response);
    })
.catch(function(error){
      //window.location = "./logout";
     console.log(error);
    });

Express:

app.use('/test', jsonParser, (req,res) =>{
   res.json(req.body);
})
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Assuming you want to post the {name: 'Justin'} object, you'll want something like

fetch('test', {
  method: 'POST',
  body: JSON.stringify({name: 'Justin'}),
  headers: new Headers({
    'Content-Type': 'application/json; charset=utf-8'
  })
})

The body parameter does not accept an array (which is what you were passing).


If you did mean to post an array, simply change the body value to

JSON.stringify([{name: 'Justin'}])
about 4 years ago · Santiago Trujillo 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!