Monday, February 25, 2019

javascript - PhantomJS GET URL Parameter



How do I retrieve the GET URL parameters when running a server under PhantomJS. Here's the code.



var webserver = require('webserver');

var server = webserver.create();


var service = server.listen(9090, function(request, response)
{
var page = require('webpage').create();

console.log('GET: ' + request.get)
console.log('POST: ' + request.post)

Answer



The Web Server module doesn't parse the parameters for you like PHP does it. You would need to do this yourself.




server.listen(9090, function(request, response) {
// parse url property to get the GET parameters
console.log('URL: ' + request.url);
console.log(" " + JSON.stringify(parseGET(request.url), undefined, 4)); // pretty print

// parse post property to get the POST parameters (message body)
console.log('BODY: ' + request.post);
};


function parseGET(url){
// adapted from http://stackoverflow.com/a/8486188
var query = url.substr(url.indexOf("?")+1);
var result = {};
query.split("&").forEach(function(part) {
var e = part.indexOf("=")
var key = part.substr(0, e);
var value = part.substr(e+1);
result[key] = decodeURIComponent(value);
});

return result;
}


The complete documentation this can be found here.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...