Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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' } 
like image 902
Tampa Avatar asked May 17 '12 21:05

Tampa


People also ask

How do I pass a query string in node JS?

querystring.parse() Method parse() method is used to parse the URL query string into an object that contains the key value pair. The object which we get is not a JavaScript object, so we cannot use Object methods like obj.

What is query string in HTTP request?

A query string is the portion of a URL where data is passed to a web application and/or back-end database. The reason we need query strings is that the HTTP protocol is stateless by design. For a website to be anything more than a brochure, you need to maintain state (store data).


1 Answers

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)

like image 121
Pero P. Avatar answered Sep 24 '22 00:09

Pero P.