I'm attempting to send an HTTP GET request to a 3rd party (not-under-my-control) web server with Node.js (using the request npm module) and am being redirected by the server to a log-in page. It refuses to send the page because I'm not logged in.
Since the redirect can't be happening on the client-side (I'm requesting the page, not parsing it), I don't understand how the web server can be checking local data or cookies to see whether or not I (my HTTP GET request) is logged in before deciding whether or not to send me the page.
My Question
In what way(s) can a web server potentially check whether or not to redirect a GET request on an existing page, before responding to the request?
My Code
var request = require('request');
function checkUsername(username) {
request({
uri : "http://www.kongregate.com/accounts/" + username + "/recently-played",
method : "GET",
timeout : 10000,
//in order to avoid being redirected to the login page, I've used:
followRedirect : false,
maxRedirects : 0
}, function(error, response, body) {
if (error)
console.log("Player does not exist");
else {
console.log("Player exists!");
}
}
});
}
checkUsername("jonathan_t0dd");
This avoids a successful redirect, but serves the html:
<html><body>You are being <a href="http://www.kongregate.com/session/new">redirected</a>.</body></html>
I would like to understand this scenario in a way that might allow me to understand how I can offer the server any data that it might require for successful serving of the asset that I'm requesting.
I don't understand how the web server can be checking local data or cookies to see whether or not I (my HTTP GET request) is logged in before deciding whether or not to send me the page.
Cookies are sent to the server with every HTTP request. That is the point of cookies. So it is, almost certainly, just looking at the cookies.
The browser will send cookies to the server, so that the web server knows whether you have log in or not.
If you want to do it manually,you can watch what cookies the site set in your browser and add them in your request.
Module request has api to set cookies,for example:
var j = request.jar()
var cookie = request.cookie('your_cookie_here')
j.setCookie(cookie, uri);
request({url: 'http://www.google.com', jar: j}, function () {
request('http://images.google.com')
})
Visit request documents to see detail.