通过柴发布请求 [英] Post request via Chai

查看:88
本文介绍了通过柴发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试向我的节点JS服务器发出请求,该服务器接受发布/发布调用.我尝试通过chai通过邮寄发送的参数在服务器(req.body.myparam)上不可见.
我已经尝试过以下发布请求,但最终没有结果:-


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:-

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.

Above code goes to myparam is missing.

请让我知道执行此操作的最佳方法是什么.
预先感谢.

Please let me know what is the best way to do the same.
Thanks in Advance.

推荐答案

按照您的编写方式,我假设您使用的是 chai-http 软件包. .field()函数在 chai-http 中不起作用.另一个用户在此处指出了这一点,并在

The way you have written, I assume that you used chai-http package. The .field() function does not work in chai-http. Another user pointed it out here and opened an issue on github.

这是你应该怎么写的:

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

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

Here is the full code that successfully passes parameters to the server:

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;

这篇关于通过柴发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆