2

I am currently trying to send some request with the npm request module. The normal callback variant works very well but I am not able to do the same with async await.

At first I tried to do it with 'request-promise-native' module but I am not able to even run a normal promise example.

var request = require('request-promise-native');
request(login)
        .then(function (response) {
            console.log("Post succeeded with status %d", response.statusCode);})
        .catch(function (err) {
            console.log("Error");
        });

I am not sure what I do wrong but the .then function is called and the response attribute is completely empty. If I look in webstorm debug I see only giberisch � inside that value. screenshot:

enter image description here

My final goal is to use the npm request module like that:

var result = await request(login);

And in the result is either the response or an error if that is not possible I also can use the promise only variant.

Can somebody tell me what I do wrong or how to do it correctly?

Regards Ruvi

Edit: Ok I found out what the first problem was. The server I was contacting send gzip info and I needed to put gzip = true in my options object and now I get a readable answer. But my problem is not solved:

if I use:

req(login, function(error, response, body){
        if (error)
         console.log(error);

        console.log(body);
        console.log(response.statusCode);
    });

I get the complete request and response object. From the Request module. If I use:

 var result = await req(login);

I get as result the request object but without the response object and response header information.

This returns me only the response body only one variable is filled:

request(login)
        .then(function (response, body) {
            console.log(response);})
        .catch(function (err) {
            console.log("Error");
        });

How do I get promise and await to return me the whole request + response object?

Ruvi
  • 253
  • 4
  • 15

1 Answers1

3

To get the complete response object you have to add: resolveWithFullResponse=true to the your options object that you pass to the request function

if you use:

var request = require('request-promise-native');

var result = await request(login);
//or
request(login)
        .then(function (response, body) {
            console.log(response);})
        .catch(function (err) {
            console.log("Error");
        });

and both works fine and the complete request/response object is returned.

Ruvi
  • 253
  • 4
  • 15