Post request via Chai

SCJP1.6 PWR picture SCJP1.6 PWR · Feb 29, 2016 · Viewed 35.1k times · Source


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) {

and

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

Node JS code is given below:-

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.

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

Answer

C00bra picture C00bra · May 16, 2016

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.

Here is how you could have written:

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