40

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' }
Asherah
  • 18,948
  • 5
  • 53
  • 72
Tampa
  • 75,446
  • 119
  • 278
  • 425

4 Answers4

48

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)

Pero P.
  • 25,813
  • 9
  • 61
  • 85
  • 1
    How do I get the raw? request.query == { tt: 'gg' }. I want tt=gg. – Tampa May 17 '12 at 22:46
  • 1
    That's what the answer does. You may want to note that `query` above is not `request.query`. The latter is a member added by express. – Pero P. May 17 '12 at 22:48
  • I do not use express. I need to be lightweight – Tampa May 17 '12 at 23:02
  • Sorry, in that case you need to add more code as `query` is not a member of the `request` object in the core node library. That's why I thought you were using express. Regardless, the answer just uses the core node modules. – Pero P. May 17 '12 at 23:07
  • @Tampa: what do you even mean by "lightweight"? What do you really want, and how is one thing vs. another lightweight? – Asherah May 18 '12 at 00:40
  • 3
    Use of the `parse` method is [discouraged](https://nodejs.org/dist/latest-v14.x/docs/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost) now. Use `new URL(req.url).search` instead, or `new URL(req.url, 'https://yourbasehere.com/').search` if you're not sure whether `reg.url` is absolute or relative. See: https://nodejs.org/dist/latest-v14.x/docs/api/url.html#url_url_search – Chris Bartley Sep 15 '21 at 21:19
  • For security stuff you can pass option like this `require('url').parse(request.url, true).query` – Athachai Jan 05 '22 at 11:43
7

You can use req.originalUrl in express 3.x+. (Older versions can use req.url from node's http module.) This should produce the raw string, excluding the ?:

var query_index = req.originalUrl.indexOf('?');
var query_string = (query_index>=0)?req.originalUrl.slice(query_index+1):'';
// 'tt=gg&hh=jj' or ''

Note that if you have a # to denote the end of the query, it will not be recognized.

If you want to pass the string along to a new URL, you should include the ?:

var query_index = req.originalUrl.indexOf('?');
var query_string = (query_index>=0)?req.originalUrl.slice(query_index):'';
// '&tt=gg&hh=jj' or ''
res.redirect('/new-route'+query_string);
// redirects to '/newroute?tt=gg&hh=jj'
ki9
  • 5,183
  • 5
  • 37
  • 48
  • 1
    `req.url.split('?')[1]` would work incorrectly for **valid** URLs, containing more than one `?` symbol. – Alexander Gonchiy Apr 19 '16 at 15:01
  • Hot damn, you're right! But the last `?` is always gonna be the start of the query, right? Let me fix that right quick... – ki9 Apr 19 '16 at 23:33
  • 1
    Nope, [the _first_ `?` always marks the start of the query](http://stackoverflow.com/a/2924187/3006854). So I'll switch to `indexOf()`. – ki9 Apr 19 '16 at 23:42
2

Express uses parseurl which stores _parsedUrl in the request object. Original string query can be accessed via request._parsedUrl.query.

WunderBart
  • 1,355
  • 1
  • 9
  • 9
  • 3
    `req.parsedUrl` is not part of the documented Express API and is thus subject to change in a future release and break your code. – Mark Stosberg Oct 09 '18 at 10:12
2
var queryString = Object.keys(request.query).map(key => key + '=' + request.query[key]).join('&');

or

var queryString = require('querystring').stringify(request.query);
user108828
  • 1,822
  • 1
  • 14
  • 21
  • The first approach can lead to security issues as you do not escape characters. You should at least `encodeURIComponent(...)` both the keys and values. Also it will not work for arrays and objects passed in query string. But the second option is secure :) – Edgar P-Yan Mar 06 '21 at 15:32