简体   繁体   中英

x-www-form-urlencoded post parameters (body) in frisby npm not working

I'm trying to test rest endpoint ' http://xxxxxxx/j_spring_security_check ' to get authentication with frisby npm package.

I am able to work in postman, by selecting request body as 'x-www-form-urlencoded' tab and given my app credentials like key-value, its working fine as expected. But in frisby npm I am unable to set request body as 'x-www-form-urlencoded'. I'm unable to login with this script.

Please help me in this or any other alternative suggestions would be great.

Here is my code:



var frisby7=require('frisby');
const qs = require('qs');


describe('API reference', function() {
    var baseURL='http://xxxxxx/j_spring_security_check';

 it('Simple Test with post url-encode form body request ', function() {
console.log("**********")
        frisby7.globalSetup({
            request: {
                headers:{'Content-Type':'application/x-www-form-urlencoded'}
            // headers: { 'X-Ms-Source':'api','X-Ms-Format':'xml','Authorization':'Basic c2hyZXlhIGdveWFsOm0jbWY4cDlMZ2ZAMU1xUTg='}
            }
            });
return frisby7.post(baseURL,
    {
        form: { j_username:'xxxx@xxxxx.com', j_password:'xxxx' }
    }).then(function (res) { // res = FrisbyResponse object
        console.log('status '+res.status);
        console.log('body '+res.body);
        //return res;
      }); 
});

You are currently sending the object in the body as if you were using 'multipart/form-data' . To send the request as 'application/x-www-form-urlencoded' you need to URI encode each property and then post them as a querystring

Try it like this

var objToSend = { j_username:'xxxx@xxxxx.com', j_password:'xxxx' };
var uriObj = Object.keys(objToSend).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(objToSend[key])).join('&');
var url = baseURL + '?' + uriObj
frisby7.post(url);

Try something like this:

var frisby = require("frisby");
const Joi = frisby.Joi;

var req1 = {
    method: "get",
    url: "pass url here", 
    headers : {
        "Accept": "application/json", 
        "content-type" : "application/json",
        'Authorization': 'Basic ' + Buffer.from(username + ":" + password).toString('base64') // pass username and password for //validation
    },
    body: {}
};

describe('spec file name', function () {
    it("spec file name" + dateTime, function(){
        return frisby
            .setup({ request: { headers : req1.headers } })     
            .get(req1.url)
            .expect("status", 200)
            .expect("header", "Content-Type", "application/json; charset=utf-8")
            .expect("jsonTypes", {
                "message": Joi.string()
            })  
            .then(function(res) {
                var body = res.body;
                body = JSON.parse(body);
                expect(body.message).toBeDefined();
            })
            .then(function(res) {
                var body = res.body;
                body = JSON.parse(body);

                var req2 = {
                    method: "put",
                    url: "pass url here",
                    headers : {
                        "Accept": "application/json", 
                        "content-type" : "application/json",
                        "Authorization": "JWT " + Token  // anything that you using to authenticate
                    },
                    body: {}
                };
                return frisby
                    .setup({ request: { headers : req2.headers } })
                    .put(req2.url)
                    .expect("status", 200)
                    .expect("header", "content-type", "application/json; charset=utf-8")
                    .expect("jsonTypes", {
                        "message": Joi.string()
                    })  
                    .then(function(res) {
                        var body = res.body;
                        body = JSON.parse(body);
                        expect(body.message).toBeDefined();
                    })          
            });             
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM