node.js 通过柴发布请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35697763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Post request via Chai
提问by SCJP1.6 PWR
I am trying to make a request to my node JS server which accepts post/put call. The parameters I am trying to send with post call via chai is not visible on server (req.body.myparam).
I have tried with below post request but ended with not results:-
我正在尝试向接受 post/put 调用的节点 JS 服务器发出请求。我试图通过 chai 通过 post call 发送的参数在服务器上不可见(req.body.myparam)。
我尝试过以下帖子请求,但没有结果:-
var host = "http://localhost:3000";
var path = "/myPath";
chai.request(host).post(path).field('myparam' , 'test').end(function(error, response, body) {
and
和
chai.request(host).post(path).send({'myparam' : 'test'}).end(function(error, response, body) {
Node JS code is given below:-
Node JS 代码如下:-
app.put ('/mypath', function(req, res){ //Handling post request to create league
createDoc (req, res);
})
app.post ('/mypath', function(req, res){ //Handling post request to create league
createDoc (req, res);
})
var createDoc = function (req, res) {
var myparam = req.body.myparam; //league id to create new league
if (!myparam) {
res.status(400).json({error : 'myparam is missing'});
return;
}
};
Above code goes to myparam is missing.
上面的代码转到 myparam 丢失。
Please let me know what is the best way to do the same.
Thanks in Advance.
请让我知道这样做的最佳方法是什么。
提前致谢。
回答by C00bra
The way you have written, I assume that you used chai-httppackage. The .field()function does not work in chai-http. Another user pointed it out hereand opened an issue on github.
你写的方式,我假设你使用了chai-http包。该点域()函数不工作柴HTTP。另一个用户在这里指出并在github上打开了一个问题。
Here is how you could have written:
你可以这样写:
.set('content-type', 'application/x-www-form-urlencoded')
.send({myparam: 'test'})
Here is the full code that successfullypasses parameters to the server:
这是成功将参数传递给服务器的完整代码:
test.js
测试.js
'use strict';
var chai = require('chai');
var chaiHttp = require('chai-http');
chai.use(chaiHttp);
describe('Test group', function() {
var host = "http://" + process.env.IP + ':' + process.env.PORT;
var path = "/myPath";
it('should send parameters to : /path POST', function(done) {
chai
.request(host)
.post(path)
// .field('myparam' , 'test')
.set('content-type', 'application/x-www-form-urlencoded')
.send({myparam: 'test'})
.end(function(error, response, body) {
if (error) {
done(error);
} else {
done();
}
});
});
});
server.js
服务器.js
'use strict';
var bodyParser = require("body-parser"),
express = require("express"),
app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.put ('/mypath', function(req, res){ //Handling post request to create league
createDoc (req, res);
});
app.post ('/mypath', function(req, res){ //Handling post request to create league
createDoc (req, res);
});
var createDoc = function (req, res) {
console.log(req.body);
var myparam = req.body.myparam; //league id to create new league
if (!myparam) {
res.status(400).json({error : 'myparam is missing'});
return;
}
};
app.listen(process.env.PORT, process.env.IP, function(){
console.log("SERVER IS RUNNING");
});
module.exports = app;
回答by Green
I found two ways to solve the issue with empty req.body.
我找到了两种解决空问题的方法req.body。
bodyas a form data.put('/path/endpoint') .type('form') .send({foo: 'bar'}) // .field('foo' , 'bar') .end(function(err, res) {} // headers received, set by the plugin apparently 'accept-encoding': 'gzip, deflate', 'user-agent': 'node-superagent/2.3.0', 'content-type': 'application/x-www-form-urlencoded', 'content-length': '127',bodyasapplication/json.put('/path/endpoint') .set('content-type', 'application/json') .send({foo: 'bar'}) // .field('foo' , 'bar') .end(function(err, res) {} // headers received, set by the plugin apparently 'accept-encoding': 'gzip, deflate', 'user-agent': 'node-superagent/2.3.0', 'content-type': 'application/json', 'content-length': '105',
body作为表单数据.put('/path/endpoint') .type('form') .send({foo: 'bar'}) // .field('foo' , 'bar') .end(function(err, res) {} // headers received, set by the plugin apparently 'accept-encoding': 'gzip, deflate', 'user-agent': 'node-superagent/2.3.0', 'content-type': 'application/x-www-form-urlencoded', 'content-length': '127',body作为application/json.put('/path/endpoint') .set('content-type', 'application/json') .send({foo: 'bar'}) // .field('foo' , 'bar') .end(function(err, res) {} // headers received, set by the plugin apparently 'accept-encoding': 'gzip, deflate', 'user-agent': 'node-superagent/2.3.0', 'content-type': 'application/json', 'content-length': '105',
In both cases I use .send({foo: 'bar'})and not .field('foo' , 'bar').
在这两种情况下,我都使用.send({foo: 'bar'})而不是.field('foo' , 'bar').
The issue apparently has nothing to do with chai-http. It is superagent's issue. And chai-httpis using superagentunder the hood.
这个问题显然与chai-http. 这是superagent问题。并在引擎盖下chai-http使用superagent。
superagenttries to play Machine Learning and make guesses for us. Here is what their docs say:
superagent尝试玩机器学习并为我们猜测。这是他们的文档所说的:
By default sending strings will set the
Content-Typetoapplication/x-www-form-urlencodedSuperAgent formats are extensible, however by default "json" and "form" are supported. To send the data as
application/x-www-form-urlencodedsimply invoke.type()with "form", where the default is "json".
默认情况下,发送字符串将设置
Content-Type为application/x-www-form-urlencodedSuperAgent 格式是可扩展的,但默认情况下支持“json”和“form”。要发送数据,
application/x-www-form-urlencoded只需.type()使用“form”调用即可,其中默认值为“json”。
request.post('/user')
.type('form')
.send({ name: 'tj' })
.send({ pet: 'tobi' })
.end(callback)
chai-httpbiggest fault is that they didn't document their plugin properly. You have to search for answers all over the Internet and not on chai-httpGitHub page where it must be.
chai-http最大的错误是他们没有正确记录他们的插件。您必须在整个 Internet 上搜索答案,而不是在chai-http必须在的GitHub 页面上搜索。

