1

I'm making an http post request to my express web server sending dummy data as json. It correctly receives the request and can send back a json object, but for some reason it can't manage to access the post request body.

I'm using this code for express:

const express = require('express');
const app = express();
const port = 3000;

app.post('/test', (req, res) => {
  console.log(req.body);
  res.json({"some": "thing"});
});

app.listen(port, () => {
  console.log(`Listening at http://localhost:${port}`)
});

And this is the code of the request:

const req = http.request({
    hostname: '127.0.0.1',
    port: 3000,
    path: '/test',
    method: 'POST',
    json: {
        url: "https://www.nothing.com",
        name: "hello"
    }
}, res => {
    console.log(`statusCode: ${res.statusCode}`)
  
    res.on('data', d => {
      process.stdout.write(d)
    })
  })
  
  req.on('error', error => {
    console.error(error)
  })
  
  req.end()

As you can see I'm running this locally. The client receives a status code 200 and the json {"some": "thing"} sent by the server, but the server gets "undefined" from req.body. I tried using:

headers: {
   'Content-Type': 'application/json'
}
body: JSON.stringify({
            url: "https://www.nothing.com",
            name: "hello"
        })

instead of json directly in the request options, but to no avail. I even tried using app.use(express.json()); as someone suggested.

What is the problem?

shin
  • 333
  • 3
  • 11

2 Answers2

1

Apparently the way I was doing the post request was not correct, I had to send the body in a separate line with req.write(), like this:

const http = require('http');
const data = JSON.stringify({ //<--- data to send as body
    url: "https://www.nothing.com",
    name: "hello"
});

const req = http.request({
    hostname: '127.0.0.1',
    port: 3000,
    path: '/test',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    }
}, res => {
    console.log(`statusCode: ${res.statusCode}`);
  
    res.on('data', d => {
      process.stdout.write(d);
    })
  })
  
  req.on('error', error => {
    console.error(error);
  })
  req.write(data); //<--- this line
  req.end();
shin
  • 333
  • 3
  • 11
0

You have to add body-parser middleware http://expressjs.com/en/resources/middleware/body-parser.html

req.body empty on posts

  • I read on another answer that body-parser is not needed from a certain version of express onward, you just need to use app.use(express.json()) as body parser. Either way I already used the module you suggested and it still didn't work. – shin Jan 11 '21 at 18:08
  • https://stackoverflow.com/questions/24543847/req-body-empty-on-posts –  Jan 11 '21 at 18:11
  • body-parser is need but you don't need to install it separately –  Jan 11 '21 at 18:13
  • yes, as I said I already used the parser as explained in your last link: app.use(express.json()); app.use(express.urlencoded()); but I still get an empty object. Have you tested my code? – shin Jan 11 '21 at 18:21