简体   繁体   中英

How to get only argument value from prototype function

Below is my constructor function "Req" and prototype function "get" to make http request.

 var request = require('request'); function Req(url, type, body) { this.url = url; this.username = "##############"; this.password = "##############"; this.type = type; this.body = body this.authenticationHeader = "Basic " + new Buffer(this.username + ":" + this.password).toString("base64"); } Req.prototype.get = function (callback) { request( { url: this.url, headers: { "Authorization": this.authenticationHeader } }, function (error, response, body) { if (error) { console.log("Error in Get Request is " + error) } else if (body) { console.log("******************GET Response is****************" + "\\n" + body) } callback(); } ); } 
This is working fine and I am calling this prototype function like this in Cucumber

 var testObject; Given('I have request with {string}, {string} and {string}', function (url, type, body) { testObject = new Req(url, type, body) }); When('the request is triggered', function (callback) { testObject.get(callback); }) 

But I want to show only response value that is "Body" within Then Step

 Then('I should get the result', function () { How to do here?? }); 

Appreciate your help.

Pass the error and body values to your callback so it can see those values.

Req.prototype.get = function(callback) {
    request({
            url: this.url,
            headers: {
                "Authorization": this.authenticationHeader
            }
        }, function(error, response, body) {
            if (error) {
                console.log("Error in Get Request is " + error)
                callback(error);
            } else {
                console.log("******************GET Response is****************" + "\n" + body)
                callback(null, body);
            } 
        }
    );
}

someObj.get(function(err, body) {
    if (!err) {
        console.log(body);
    }
});

FYI, I don't know about protractor and cucumber so I've shown you a pure Javascript way to do this.

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