'Node.js http request returning body/msg empty

I am trying to use the requestjs package to post data and wait for a response. But I the body response is always undefined.

var request = require('request');

request({
      method: "POST",
      baseUrl: "https://255.255.255",
      uri: "/login",
form: { 
      username: "username",
      password: "password",
      autologin: "true"}},
function(body, msg, err){ 
      console.log(body); console.log(msg); 
})

Edit: Again, the undefined body was caused by a privacy policy.



Solution 1:[1]

I think you are getting confused with Promise and non-promise request package. As per your example, $ajax returns Promiseified response and you directly get the data from the response of the ajax request. You are expecting that request package should also give you data directly, which is not correct.

Actually, you can solve your issue in two ways:

Sol. 1: Use proper callback function arguments and you must get data in the third argument of the callback function. Such as:

var request = require('request');

request({
    method: "POST",
    baseUrl: "https://255.255.255",
    uri: "/login",
    form: {
      username: "username",
      password: "password",
      autologin: "true"
    }
  },
  function (error, httpResponse, body) {
    if (error) {
      console.error(error);
    }
    console.log(httpResponse.statusCode);
    console.log(body);
  });

Sol. 2: Use request-promise NPM package (download it from here) and get pomisified response. For example:

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

const getData = async () => {
  return new Promise((resolve, reject) => {
    const options = {
      method: "POST",
      baseUrl: "https://255.255.255",
      uri: "/login",
      form: {
        username: "username",
        password: "password",
        autologin: "true",
        resolveWithFullResponse: true, // Returns full response. To get only data don't use this property or mark it false.
      }
    };

    // Get whole Response object.
    const response = await request(options);

    // Returns the Promise.Resolve or Reject based on response.
    if (response.statusCode < 200 || response.statusCode > 300) {
      const errorMsg = 'Error occurred while POSTing the request. Got status: ' + response.status;
      console.error(errorMsg);

      // Reject the promise. Should be caught.
      return reject(errorMsg);
    }

    const responseBody = response.body;
    console.log(responseBody);

    // Return the response.
    return resolve(responseBody);
  })
}

Above implementation will return a promise for the method getData() being called.

NOTE: The statement const response = await request(options); will return whole response object if resolveWithFullResponse: true, is used in the options JSON object. If you need only response body or data don't mention resolveWithFullResponse property in the options or assign value false to it. By default the value of resolveWithFullResponse is false.

Solution 2:[2]

The format for the callback is (err,response,body); maybe that is why you are getting a empty body and response. You can refer here for details.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 S.Mishra
Solution 2 Arjun Agarwal