Search code examples
node.jsexpress

How to get the unparsed query string from a http request in Express


I can use the below to get the query string.

  var query_string = request.query;

What I need is the raw unparsed query string. How do I get that? For the below url the query string is { tt: 'gg' }, I need tt=gg&hh=jj etc....

http://127.0.0.1:8065?tt=gg

Server running at http://127.0.0.1:8065
{ tt: 'gg' }

Solution

  • You can use node's URL module as per this example:

    require('url').parse(request.url).query
    

    e.g.

    node> require('url').parse('?tt=gg').query
    'tt=gg'
    

    Or just go straight to the url and substr after the ?

    var i = request.url.indexOf('?');
    var query = request.url.substr(i+1);
    

    (which is what require('url').parse() does under the hood)