3

I'm trying to write a stupidly simple Hello World program in Express that outputs some basic data about the current HTTP request.

For POST requests, I'd like to see the raw POST body.

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

function handleRequest(req, res) {
    console.log('\n-- INCOMING REQUEST AT ' + new Date().toISOString());
    console.log(req.method + ' ' + req.url);
    console.log(req.body);
    res.send('Hello World!');
}

app.all('/*', (req, res) => handleRequest(req, res));
app.listen(3000, () => console.log('Example app listening on port 3000!'));

When I fire off any type of POST request from Postman, req.body is set to undefined. Why is req.body empty? How can I print out the raw POST data? I don't need a parsed version of the POST body, just the raw data.

Pieter
  • 893
  • 1
  • 8
  • 20
  • 1
    Possible duplicate of [What does body-parser do with express?](https://stackoverflow.com/questions/38306569/what-does-body-parser-do-with-express) - TL;DR, you need the `body-parser` middleware installed to be able to handle request bodies. – Joe Clay Feb 28 '18 at 12:46
  • use body-parser https://www.npmjs.com/package/body-parser – zb22 Feb 28 '18 at 12:46

2 Answers2

4

In order to read the body of a post request you need body-parser. If you also need to parse multipart/form-data you need multer.

after you npm install them:

const express = require('express');
const multer = require('multer');
const bodyParser = require('body-parser');
const upload = multer();
const app = express();

// create application/json parser
app.use(bodyParser.json());

// create application/x-www-form-urlencoded parser
app.use(bodyParser.urlencoded({ extended: false }));

function handleRequest(req, res) {
    console.log('\n-- INCOMING REQUEST AT ' + new Date().toISOString());
    console.log(req.method + ' ' + req.url);
    console.log(req.body);
    res.send('Hello World!');
}

app.post('/*', upload.any(), (req, res) => handleRequest(req, res));
app.all('/*', (req, res) => handleRequest(req, res));
app.listen(3000, () => console.log('Example app listening on port 3000!'));
Giannis Mp
  • 1,291
  • 6
  • 13
  • This still seems to produce empty objects in some cases. It doesn't work when I use the 'form-data' option in Postman. – Pieter Feb 28 '18 at 13:04
  • Can you try with extended: true? If you want to post multipart/form-data you need multer - https://www.npmjs.com/package/multer – Giannis Mp Feb 28 '18 at 13:14
  • 1
    Still not the raw POST data, but this will also cover my needs. Thanks! – Pieter Feb 28 '18 at 13:37
3
  1. If you required parsing of url-encoded (non-multipart) form data, as well as JSON, try adding:

    var bodyParser = require('body-parser');
    
    
    // Put these statements before you define any routes.
    app.use(bodyParser.urlencoded({ extended: true}));
    app.use(bodyParser.json());
    
  2. To handle multi-part form data, the bodyParser.urlencoded() body parser will not work. you can refer this for alternatives of extracting data from form-data

GansPotter
  • 761
  • 5
  • 8
  • Would you mind having a look at this one please? Am I doing something wrong? https://stackoverflow.com/questions/76064309/unexpected-end-of-form-at-callfinal-error – Dzsonah Apr 24 '23 at 14:32