如何在Express中强制解析请求正文作为纯文本而不是json? [英] How to force parse request body as plain text instead of json in Express?
问题描述
我正在使用这样的nodejs + Express(v3):
app.use(express.bodyParser());
app.route('/ some / route',function(req,res){
var text = req.body; //我希望文本是一个字符串,但它是一个JSON
});
我检查了请求标头,缺少内容类型。即使Content-Type是text / plain,它正在将其解析为JSON。有没有告诉中间件总是解析身体是纯文本字符串而不是json?以前, req
的早期版本曾经有 req.rawBody
可以解决这个问题,但现在不再这样了。在Express中强制解析为纯文本/字符串的最简单的方法是什么?
如果您删除使用 bodyParser()
应该是文字。您可以查看 bodyParser
文档以获取更多信息: http:
pre> app.use(express.bodyParser());
编辑:
对吧您可以在此期间创建自己的 rawBody
中间件。但是,您仍然需要禁用 bodyParser()
。注意: req.body
仍然是未定义
。
这是一个演示:
app.js
code> var express = require('express')
,http = require('http')
,path = require('path')
,util = UTIL');
var app = express();
function rawBody(req,res,next){
req.setEncoding('utf8');
req.rawBody ='';
req.on('data',function(chunk){
req.rawBody + = chunk;
});
req.on('end',function(){
next();
});
}
app.configure(function(){
app.set('port',process.env.PORT || 3000);
app.use (rawBody);
//app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
app.post('/ test',function(req,res){
console.log(req.is('text / *'));
控制台。 log(req.is('json'));
console.log('RB:'+ req.rawBody);
console.log('B:'+ JSON.stringify(req.body ));
res.send('got it');
});
http.createServer(app).listen(app.get('port'),function(){
console.log(Express server listening on port+ app.get 'port'));
});
test.js
var request = require('request');
请求({
方法:'POST',
uri:'http:// localhost:3000 / test',
body:{'msg' '
json:true
},function(error,response,body){
console.log('code:'+ response.statusCode);
控制台.log(body);
})
希望这有帮助。 b $ b
I am using nodejs + Express (v3) like this:
app.use(express.bodyParser());
app.route('/some/route', function(req, res) {
var text = req.body; // I expect text to be a string but it is a JSON
});
I checked the request headers and the content-type is missing. Even if "Content-Type" is "text/plain" it is parsing as a JSON it seems. Is there anyway to tell the middleware to always parse the body as a plain text string instead of json? Earlier versions of req
used to have req.rawBody
that would get around this issue but now it does not anymore. What is the easiest way to force parse body as plain text/string in Express?
If you remove the use of the bodyParser()
middleware, it should be text. You can view the bodyParser
docs for more info: http://www.senchalabs.org/connect/middleware-bodyParser.html
Remove this line:
app.use(express.bodyParser());
EDIT:
Looks like you're right. You can create your own rawBody
middleware in the meantime. However, you still need to disable the bodyParser()
. Note: req.body
will still be undefined
.
Here is a demo:
app.js
var express = require('express')
, http = require('http')
, path = require('path')
, util = require('util');
var app = express();
function rawBody(req, res, next) {
req.setEncoding('utf8');
req.rawBody = '';
req.on('data', function(chunk) {
req.rawBody += chunk;
});
req.on('end', function(){
next();
});
}
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.use(rawBody);
//app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
app.post('/test', function(req, res) {
console.log(req.is('text/*'));
console.log(req.is('json'));
console.log('RB: ' + req.rawBody);
console.log('B: ' + JSON.stringify(req.body));
res.send('got it');
});
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
test.js
var request = require('request');
request({
method: 'POST',
uri: 'http://localhost:3000/test',
body: {'msg': 'secret'},
json: true
}, function (error, response, body) {
console.log('code: '+ response.statusCode);
console.log(body);
})
Hope this helps.
这篇关于如何在Express中强制解析请求正文作为纯文本而不是json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!