parsing query string in haxe

400 views
Skip to first unread message

TroyWorks

unread,
Oct 4, 2014, 11:56:59 PM10/4/14
to haxe...@googlegroups.com
      var querystring  = window.location.search.substring(1);
        trace
("querystring '" + querystring + "'");
       
var urlParams = parseQueryString(querystring);
   
   
public function parseQueryString(qs:String):Map {
       
var a:Array<String>  = qs.split('&');
       
var pl  = ~/\+/g;  // Regex for replacing addition symbol with a space
       
var b = new Map<String,String>();
       
for ( i in 0...a.length)
       
{
           
var p=a[i].split('=');
           
var k:String =p[0];

           
if (p.length == 1){
                trace
("p " + k + "== *Blank*" );
                b
.set(k, "");
           
} else{
               
var v:String =StringTools.urlDecode(pl.split(p[1]).join(" "));
                trace
("p " + k + "==" +v );
                b
.set(k, v);
           
}
       
}
       
return b;
   
}

Apologies if this already exists somewhere, couldn't find any examples in searching or docs.  Adding it here to better show up in search results.

Pier Bover

unread,
Oct 5, 2014, 12:07:50 AM10/5/14
to haxe...@googlegroups.com

TroyWorks

unread,
Oct 5, 2014, 11:05:37 PM10/5/14
to haxe...@googlegroups.com
Cool thanks for pointing that out.  Pity it's not documented with other keyterms like "query string" that are commonly used to find it.

Axel Huizinga

unread,
Oct 6, 2014, 1:04:40 AM10/6/14
to haxe...@googlegroups.com
Reading there:
Returns the current page GET and POST parameters (only GET parameters for Javascript)

I wonder if the class is not meant to be used for server side js as well?

Axel
--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.


Juraj Kirchheim

unread,
Oct 6, 2014, 3:46:18 AM10/6/14
to haxe...@googlegroups.com
On Mon, Oct 6, 2014 at 7:03 AM, Axel Huizinga <ax...@cunity.me> wrote:

Am 06.10.2014 05:05, schrieb TroyWorks:
Cool thanks for pointing that out.  Pity it's not documented with other keyterms like "query string" that are commonly used to find it.

On Saturday, October 4, 2014 9:07:50 PM UTC-7, Pier Bover wrote:
Reading there:
Returns the current page GET and POST parameters (only GET parameters for Javascript)

I wonder if the class is not meant to be used for server side js as well?

If you mean node.js then no, since in a node app you have multiple HTTP requests, instead of having just one global request, which only works in the "classical" way PHP applications handle each request in isolated contexts.

Best,
Juraj

Axel Huizinga

unread,
Oct 6, 2014, 5:34:07 AM10/6/14
to haxe...@googlegroups.com
why can't each of those multiple requests handled the same way with full api?

Best,
Juraj

Juraj Kirchheim

unread,
Oct 6, 2014, 6:34:14 AM10/6/14
to haxe...@googlegroups.com
The haxe.web.Request class object is global. Its API is fundamentally coupled with the assumption that only one request exists in all of your current context. That does not hold in node.

In node, you have instances of `IncomingMessage` that represent each request - see here: http://nodejs.org/api/http.html#http_http_incomingmessage

Similarly, you also cannot simply respond by `Sys.print`, because it is no clear which of the (con)currently processed requests you are responding to.

So yes, you could have a similar API, but there is not really a simple way to define it solely on global (class) objects.

Does that answer the question?

Best,
Juraj

Axel Huizinga

unread,
Oct 6, 2014, 7:03:18 AM10/6/14
to haxe...@googlegroups.com

Am 06.10.2014 12:34, schrieb Juraj Kirchheim:
The haxe.web.Request class object is global. Its API is fundamentally coupled with the assumption that only one request exists in all of your current context. That does not hold in node.

In node, you have instances of `IncomingMessage` that represent each request - see here: http://nodejs.org/api/http.html#http_http_incomingmessage

Similarly, you also cannot simply respond by `Sys.print`, because it is no clear which of the (con)currently processed requests you are responding to.

So yes, you could have a similar API, but there is not really a simple way to define it solely on global (class) objects.

Does that answer the question?

Yes - thank you :)

Csomák Gábor

unread,
Feb 17, 2016, 11:06:58 AM2/17/16
to Haxe
Hi All!

Google threw me this answer, cleaned it up a bit, added types, and now sharing it if anyone needs:

package url;

class UrlProperties {

private static var map:Map<String, String>;

public static function getProperty(queryId:String):String {
if (map == null) {
map = parseQueryMap(getQueryString());
}
return map.get(queryId);
}

private static function getQueryString():String {
return untyped __js__("window.location.search");
}

private static function parseQueryMap(qs:String):Map<String, String> {
var splitArray:Array<String> = qs.split('&');
var regexPlus = ~/\+/g; // Regex for replacing addition symbol with a space
var resultMap = new Map<String,String>();
for (i in 0...splitArray.length) {
var splitByEqual = splitArray[i].split('=');
var key:String = splitByEqual[0];

if (splitByEqual.length == 1) {
// ..&a=&b.. (right side blank)
resultMap.set(key, "");
} else {
var value:String = StringTools.urlDecode(regexPlus.split(splitByEqual[1]).join(" "));
resultMap.set(key, value);
}
}
return resultMap;
}

}
Reply all
Reply to author
Forward
0 new messages