获取 POST 参数不起作用
Getting POST parameters not working
我正在尝试使用 restify 框架使用 PostMan 发送 post 参数(键:test,值:somevlaue)。为此,我使用了 2 种方法,但都不起作用:
第一个显示此错误:
{
"code": "InternalError",
"message": "Cannot read property 'test' of undefined"
}
第二个(已注释)仅显示错误:someerror
我是不是做错了什么?
这是我的代码:
var restify=require('restify');
var fs=require('fs');
var qs = require('querystring');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false })
var controllers = {};
var server=restify.createServer();
server.post("/get", function(req, res, next){
res.send({value: req.body.test,
error: "someerror"});
//**********METHOD TWO*********************
/*
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
var post = qs.parse(body);
res.send({
Data: post.test,
Error: "Someerror"
});
});
}
*/
});
server.listen(8081, function (err) {
if (err)
console.error(err);
else
console.log('App is ready at : ' + 8081);
});
您的 bodyparser
设置似乎有误。
根据正文解析器部分下的 the docs,您以这种方式设置解析器:
server.use(restify.bodyParser({
maxBodySize: 0,
mapParams: true,
mapFiles: false,
.....
}));
默认将数据映射到 req.params
,但您可以通过将 mapParams
选项设置为 false
[=18 来更改它并将其映射到 req.body
=]
BodyParser
Blocks your chain on reading and parsing the HTTP request body.
Switches on Content-Type and does the appropriate logic.
application/json, application/x-www-form-urlencoded and
multipart/form-data are currently supported.
使用 restify ^7.7.0,您不再需要 require('body-parser')。只需使用 restify.plugins.bodyParser():
var server = restify.createServer()
server.listen(port, () => console.log(`${server.name} listening ${server.url}`))
server.use(restify.plugins.bodyParser()) // can parse Content-type: 'application/x-www-form-urlencoded'
server.post('/your_url', your_handler_func)
我正在尝试使用 restify 框架使用 PostMan 发送 post 参数(键:test,值:somevlaue)。为此,我使用了 2 种方法,但都不起作用:
第一个显示此错误:
{
"code": "InternalError",
"message": "Cannot read property 'test' of undefined"
}
第二个(已注释)仅显示错误:someerror
我是不是做错了什么?
这是我的代码:
var restify=require('restify');
var fs=require('fs');
var qs = require('querystring');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false })
var controllers = {};
var server=restify.createServer();
server.post("/get", function(req, res, next){
res.send({value: req.body.test,
error: "someerror"});
//**********METHOD TWO*********************
/*
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
var post = qs.parse(body);
res.send({
Data: post.test,
Error: "Someerror"
});
});
}
*/
});
server.listen(8081, function (err) {
if (err)
console.error(err);
else
console.log('App is ready at : ' + 8081);
});
您的 bodyparser
设置似乎有误。
根据正文解析器部分下的 the docs,您以这种方式设置解析器:
server.use(restify.bodyParser({
maxBodySize: 0,
mapParams: true,
mapFiles: false,
.....
}));
默认将数据映射到 req.params
,但您可以通过将 mapParams
选项设置为 false
[=18 来更改它并将其映射到 req.body
=]
BodyParser
Blocks your chain on reading and parsing the HTTP request body. Switches on Content-Type and does the appropriate logic. application/json, application/x-www-form-urlencoded and multipart/form-data are currently supported.
使用 restify ^7.7.0,您不再需要 require('body-parser')。只需使用 restify.plugins.bodyParser():
var server = restify.createServer()
server.listen(port, () => console.log(`${server.name} listening ${server.url}`))
server.use(restify.plugins.bodyParser()) // can parse Content-type: 'application/x-www-form-urlencoded'
server.post('/your_url', your_handler_func)