使用Express访问原始POST数据

时间:2021-12-22 15:59:29

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

我正在尝试用Express编写一个简单得愚蠢的Hello World程序,它输出关于当前HTTP请求的一些基本数据。

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

对于POST请求,我希望看到原始的POST主体。

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.

当我向邮递员发出任何类型的邮件请求时,req。body被设置为undefined。为什么要求。身体空?如何打印原始的POST数据?我不需要对POST主体进行解析,只需要原始数据。

2 个解决方案

#1


2  

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.

为了读取post请求的主体,您需要body解析器。如果还需要解析多部分/表单数据,则需要multer。

after you npm install them:

npm安装后:

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!'));

#2


1  

  1. If you required parsing of url-encoded (non-multipart) form data, as well as JSON, try adding:

    如果需要解析url编码(非多部分)表单数据以及JSON,请尝试添加:

    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

    要处理多部分表单数据,bodyparser.urlencode()体解析器将不起作用。您可以将其作为从表单数据中提取数据的替代方法

#1


2  

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.

为了读取post请求的主体,您需要body解析器。如果还需要解析多部分/表单数据,则需要multer。

after you npm install them:

npm安装后:

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!'));

#2


1  

  1. If you required parsing of url-encoded (non-multipart) form data, as well as JSON, try adding:

    如果需要解析url编码(非多部分)表单数据以及JSON,请尝试添加:

    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

    要处理多部分表单数据,bodyparser.urlencode()体解析器将不起作用。您可以将其作为从表单数据中提取数据的替代方法