【发布时间】:2015-07-18 00:40:39
【问题描述】:
我是否需要做任何特别的事情来处理带有文本 Content-Type 的 POST 请求?
我需要处理text/csv,但是当我访问控制器中的方法时,看起来 Sails.js 试图将正文解析为 JSON:
postStuff: function(req, res) {
sails.log.info("postStuff")
sails.log.info(req.body)
sails.log.info(req.headers['content-type']);
...etc...
给我:
info: postStuff
info: {}
info: text/csv
我发现bodyParser 中间件的文档有点晦涩。
FWIW,我也尝试在请求中将Content-Type 设置为text/plain,但无济于事。
我也尝试显式添加一个文本bodyParser作为中间件,似乎没有任何效果:
http.js
module.exports.http = {
bodyParserText: require('body-parser').text(),
middleware: {
order: [
'startRequestTimer',
'cookieParser',
'session',
'myRequestLogger',
'bodyParser',
'bodyParserText',
'handleBodyParserError',
'compress',
'methodOverride',
'poweredBy',
'$custom',
'router',
'www',
'favicon',
'404',
'500'
],
...etc...
【问题讨论】:
-
请注意
bodyParserText在middleware.order列表中的排序也没有任何区别。