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?

6
  • have you tried to console.log only response and not response.statusCode ? Commented Feb 6, 2018 at 12:53
  • first, print response and see what you receive Commented Feb 6, 2018 at 12:54
  • Yes I get this if I console.log response: �VJ,-�P�R2P�Q�M-.NLO�j� �� Commented Feb 6, 2018 at 13:00
  • @Ruvi can you update your question with screenshot Commented Feb 6, 2018 at 13:08
  • seems to be an encoding problem, see if you can set the response encoding to utf8 Commented Feb 6, 2018 at 13:29

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

even then i still see the response as OP posted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.