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?

1
  • please add this line req.write(data); before req.end(); Commented May 26, 2021 at 8:07

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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

req.body empty on posts

4 Comments

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.
body-parser is need but you don't need to install it separately
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.