2016-02-29 63 views
9


我正在试图向我的节点JS服务器发出接受post/put调用的请求。我尝试通过chai发送后调用的参数在服务器(req.body.myparam)上不可见。
我曾尝试用下面POST请求,但最终有没有效果: -
通过Chai的发布请求

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丢失。

请让我知道什么是做同样的最好的方式。
在此先感谢。

+0

你可以分享端点的代码吗? –

+0

更新了代码。如果你需要其他东西,请告诉我。 –

+0

我没有看到在任何地方定义的“联盟”? – Derek

回答

13

你写的方式,我假设你用的是chai-http包。 .field()功能在chai-http中不起作用。另一位用户指出here并在github上打开了一个问题。

这里是你怎么可以这样写:

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

这里是成功将参数传递给服务器的完整代码:

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; 
+0

这对我有效。这很有趣,因为文档在发布表单数据时会说使用.field()方法,但如果您在服务器上以这种方式使用bodyParser,那么这可能不起作用? –

+0

如何在这附上一个文件?我知道这是代码.attach('imageField',fs.readFileSync('avatar.png'),'avatar.png')但我不知道在哪附上它 – Kannan

3

我找到了两种方法解决这个问题,用空的req.body

  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. 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', 
    

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

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

superagent试图玩机器学习和猜测我们。以下是他们的docs say

默认情况下发送的字符串将设置Content-Typeapplication/x-www-form-urlencoded

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

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

chai-http最大的错误是,他们没有正确地记录他们的插件。你必须通过互联网搜索答案,而不是在chai-http GitHub页面上。

+0

如何在这个文件中附加文件?我知道这是代码.attach('imageField',fs.readFileSync('avatar.png'),'avatar.png')但我不确定在哪里附加它 – Kannan

相关问题