Jay P
Jay P

Reputation: 626

NodeJs get request returns wrong data

Im building a 'man in the middle' type server. Users requests pages from my node server, my node server requests the pages from the origin server and cache's them for next time the page is requested.

I am currently trying to open a session on the origin server.

If I visit this url

www.originDomain.co.uk/ajax/[email protected]&password=password

a session is successfully open and i get a response back (from the php script) saying logged in.

I cant seem to replicate this request in node.js I am using this code:

        var data = querystring.stringify({
           username: '[email protected]',
           password: 'password'
        });

        var options = {
            host: 'www.originDomain.co.uk',
            port: 80,
            path: '/ajax/login.php',
            method: 'GET',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': Buffer.byteLength(data)
            }
        };

        var req = http.request(options, function(res) {
            res.setEncoding('utf8');
            res.on('data', function (chunk) {
                console.log("body: " + chunk);
            });
        });

        req.write(data);
        req.end();

with this call i get a response (from the PHP script) saying "please enter a user name"

I am assuming that the query string is not being sent with the request as if you just visit www.originDomain.co.uk/ajax/login.php you get the message back "please enter a user name".

Please help, been trying to solve this for 3 hours now. Iv looked at the node documentation and iv searched forums but nothing seems to fix it.

Upvotes: 0

Views: 1349

Answers (2)

Jay P
Jay P

Reputation: 626

I found the solution. To be honest with you, no idea how this fixed my issue... but it did.

        var headers = {
          'User-Agent':       'Super Agent/0.0.1',
          'Content-Type':     'application/x-www-form-urlencoded'
        }

        // Configure the request
        var options = {
            url: 'http://www.originDomain.co.uk/ajax/login.php',
            method: 'GET',
            headers: headers,
            qs: {'username': '[email protected]', 'password':'password'}
        }

        // Start the request
        req(options, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                // Print out the response body
                console.log(body)
            }
        })

Upvotes: 0

Jason Cust
Jason Cust

Reputation: 10899

It appears you are trying to send your login credentials in the body of a GET request. Also to simplify your request processing you might want to look at using request rather than node's native http.request.

Example:

var options = {
  url: 'https://www.originDomain.co.uk/ajax/login.php',
  qs: {
    username: '[email protected]',
    password: 'password'
  }
};

request(options, function(err, resp, body) {
  console.log(body);
});

That said, to begin with I would be concerned about sending the user credentials as query params in a GET request.

Upvotes: 1

Related Questions