通过Chai发送请求

我正在试图请求我的节点JS服务器接受后/投入呼叫。 我尝试通过chai发送后调用的参数在服务器(req.body.myparam)上不可见。
我已经尝试了下面的post请求,但没有结果结束:

var host = "http://localhost:3000"; var path = "/myPath"; chai.request(host).post(path).field('myparam' , 'test').end(function(error, response, body) { 

 chai.request(host).post(path).send({'myparam' : 'test'}).end(function(error, response, body) { 

节点的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; } }; 

上面的代码去myparam丢失。

请让我知道什么是做同样的最好的方式。
提前致谢。

你写的方式,我假设你使用chai-http包。 .field()函数在chai-http中不起作用。 另一个用户在这里指出,并在github上打开一个问题。

以下是你如何写:

 .set('content-type', 'application/x-www-form-urlencoded') .send({myparam: 'test'}) 

以下是成功将parameter passing给服务器的完整代码:

test.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

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

我find了两种方法来解决这个问题。

  1. 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', 
  2. 身为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', 

在这两种情况下,我使用.send({foo: 'bar'})而不是.field('foo' , 'bar')

这个问题显然与chai-http无关。 这是superagent的问题。 chai-http使用的是superagent

superagent试图玩机器学习,并为我们猜测。 以下是他们的文档说 :

默认情况下,发送string将把Content-Type设置为application/x-www-form-urlencoded

SuperAgent格式是可扩展的,但默认支持“json”和“form”。 要以application/x-www-form-urlencoded发送数据,只需使用“form”调用.type() ,默认为“json”。

  request.post('/user') .type('form') .send({ name: 'tj' }) .send({ pet: 'tobi' }) .end(callback) 

chai-http最大的错在于他们没有正确地logging他们的插件。 您必须通过互联网search答案,而不是在chai-http GitHub页面上查找答案。