garson
garson

Reputation: 1617

gibberish instead of html response as body in node.request

I'm making a HTTP request to a password protected site using the request module in npm, putting in a password, storing a cookie, then making the request once the cookie is stored and verified. I am able to get the same headers as I would on a normal browser request, but hte body itself, instead of being the HTML document I get in a browser, just looks like this:

� �Z�r�H��c��䞙��pT���Ī$3�ƾ�~Y�@�MK8���>*��?�z)�?U���ݨ�J�혳��섯tB��x��c��?�����������0�H�����V��O'�7����}���L�"˖}/ta�xn�g#�ݱ�O�����

Any ideas what might be causing this or how I can fix it?

In addition, when I run this from the command prompt, the computer "dings"

Here is the full node.js code I am running (minus the URLs/passwords/etc.)

var parsedurl1 = url.parse( urlstring1 );
var options1 = {
  hostname: parsedurl1.hostname,
  port: ( parsedurl1.port || 80 ), // 80 by default
  method: 'POST',
  path: parsedurl1.path,
  headers: { 
      'Host': hostname
      ,'User-Agent': myuseragent
      ,'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
      ,'Accept-Language':"en-US,en;q=0.5"
      ,'Accept-Encoding': "gzip, deflate"
      ,'Referer': hostname
      ,'Content-Type': "text/html; charset=utf-8"
      ,'Content-Length': Buffer.byteLength(postData)
      ,'Connection': "keep-alive"
      ,'Upgrade-Insecure-Requests': "1"},
};
var cookiefile ; 
var postData=querystring.stringify({
        'email': myemail
        ,'password':mypassword
        ,'action': 'login'
        ,'go.x': 0
        ,'go.y': 0
    })
var cookiefile;
var callback =  function ( response ) {
    // display returned cookies in header
    var setcookie = response.headers["set-cookie"];
    if ( setcookie ) {
      setcookie.forEach(
        function ( cookiestr ) {
             cookiefile = cookiestr;
             fs.writeFile(cookiefilelocation, cookiestr);
          console.log(  "COOKIE:" + cookiestr );
        }
      );
    }    
    var data = "";
    response.on(
      "data",
      function ( chunk ) { data += chunk; }
    );

    response.on(
      "end",
      function () {  
            newcookiefile =  cookiefile.substr(0, cookiefile.indexOf(";"));
            var parsedurl2 = url.parse( urlstring2 );
            var options2 = {
                url: urlstring2,
                // port: ( parsedurl2.port || 80 ), // 80 by default
                method: 'GET',
                // path: parsedurl2.path,
                headers: { 
                "Host": hostname
                ,"User-Agent": myuseragent
                ,'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
                ,'Accept-Language':"en-US,en;q=0.5"
                ,'Accept-Encoding': "gzip, deflate"
                ,'Referer':hostname
                ,'Cookie': newcookiefile
                ,'Connection': "keep-alive"
                ,'Upgrade-Insecure-Requests': "1"},
            };

            function callback3(error, response, body){
                console.log('error:', error); // Print the error if one occurred 

                console.log('statusCode:', response.headers ); // Print the response status code if a response was received 
                console.log('body:', body.substr(1,1000));
                fs.writeFile('./config/pullfiles/mostrecent.txt', body);
                }
            requestlib(options2, callback3);
      }
    );
  };
var request = http.request(options1, callback);


request.on(
  "error",
  function( err ) {
    console.error( "RERROR:" + err );
  }
);
request.write(postData);
request.end(); // let request know it is finished sending

Upvotes: 0

Views: 1278

Answers (1)

garson
garson

Reputation: 1617

I figured it out! This gibberish was caused by my lack of the 'gzip: true' option is my request(). Now the second request reads:

url: urlstring2
    ,gzip: true
    ,headers:{...

Upvotes: 5

Related Questions