3

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)

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'll let you figure out how to parse POST parameters. It shouldn't be too hard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.