Reading query string parameters

937 views
Skip to first unread message

fils

unread,
Apr 18, 2012, 11:39:40 AM4/18/12
to General Dart Discussion
So I feel like a total noob, but for the life of me I can't find this.

Is there a simple/default way to get a map of query values in
Dart?

foo.com#foo=bar&x=y so I get foo:bar and x:y in a map?

Mark Bennett

unread,
Apr 18, 2012, 5:03:23 PM4/18/12
to General Dart Discussion
I'm a newb here to so please correct me if I'm wrong, but if you're
running the HttpServer in the Dart VM then the
HttpRequest#queryParameters is a Map you can access.

http://api.dartlang.org/io/HttpRequest.html#get:queryParameters

Otherwise, if you've just got access to the string you could do
something like this:

String query = "foo=bar&x=y";

// Split the query into pieces
List pieces = query.split("&").map((e) => e.split("="));

// Convert the pieces into a map
Map params = {};
pieces.forEach((piece) => params[piece[0]] = piece[1]);

print(params);

Hope that helps. Curious to see if anyone knows a better way? Perhaps
Dart needs a way to initialize a Map from a set of key / value pairs
like in other languages?

Bob Nystrom

unread,
Apr 18, 2012, 5:47:20 PM4/18/12
to Mark Bennett, General Dart Discussion
On Wed, Apr 18, 2012 at 2:03 PM, Mark Bennett <mark.f....@gmail.com> wrote:
I'm a newb here to so please correct me if I'm wrong, but if you're
running the HttpServer in the Dart VM then the
HttpRequest#queryParameters is a Map you can access.

http://api.dartlang.org/io/HttpRequest.html#get:queryParameters

Otherwise, if you've just got access to the string you could do
something like this:

 String query = "foo=bar&x=y";

 // Split the query into pieces
 List pieces = query.split("&").map((e) => e.split("="));

 // Convert the pieces into a map
 Map params = {};
 pieces.forEach((piece) => params[piece[0]] = piece[1]);

 print(params);

Hope that helps. Curious to see if anyone knows a better way?

I don't know of a better way. This seems like the kind of thing it would be really nice if our Uri class supported for you. Maybe submit a feature request so we don't forget this?

Thanks!

- bob

Istvan Soos

unread,
Apr 18, 2012, 6:03:45 PM4/18/12
to Mark Bennett, General Dart Discussion
I think this bug is related, star it if you think it covers your case
too. I've filed it after I've encountered an HTTP POST with the form
parameters in the request body. The planned library changes looked
good to me:
http://code.google.com/p/dart/issues/detail?id=2488

As for the split-workaround: you still need to html-unescape the
values like %30 and '+' encodings...

Regards,
Istvan

On Wed, Apr 18, 2012 at 2:03 PM, Mark Bennett <mark.f....@gmail.com> wrote:

Mark Bennett

unread,
Apr 18, 2012, 6:13:04 PM4/18/12
to General Dart Discussion
Good to know, thanks Bob.

I went ahead and added a feature request to Uri to parse query params
into a Map.

http://code.google.com/p/dart/issues/detail?id=2645

Is there a process for contributing a patch to add this myself?

I may have gotten a little carried away and added a few other requests
as well that stemmed from thinking about this issue.

http://code.google.com/p/dart/issues/detail?id=2641
http://code.google.com/p/dart/issues/detail?id=2642
http://code.google.com/p/dart/issues/detail?id=2643
http://code.google.com/p/dart/issues/detail?id=2644


Thanks!

-Mark

On Apr 18, 3:47 pm, Bob Nystrom <rnyst...@google.com> wrote:
> On Wed, Apr 18, 2012 at 2:03 PM, Mark Bennett <mark.f.benn...@gmail.com>wrote:
>
>
>
>
>
>
>
>
>
> > I'm a newb here to so please correct me if I'm wrong, but if you're
> > running the HttpServer in the Dart VM then the
> > HttpRequest#queryParameters is a Map you can access.
>
> >http://api.dartlang.org/io/HttpRequest.html#get:queryParameters
>
> > Otherwise, if you've just got access to the string you could do
> > something like this:
>
> >  String query = "foo=bar&x=y";
>
> >  // Split the query into pieces
> >  List pieces = query.split("&").map((e) => e.split("="));
>
> >  // Convert the pieces into a map
> >  Map params = {};
> >  pieces.forEach((piece) => params[piece[0]] = piece[1]);
>
> >  print(params);
>
> > Hope that helps. Curious to see if anyone knows a better way?
>
> I don't know of a better way. This seems like the kind of thing it would be
> really nice if our Uri <http://api.dartlang.org/uri/Uri.html> class

Bob Nystrom

unread,
Apr 18, 2012, 6:26:52 PM4/18/12
to Mark Bennett, General Dart Discussion
On Wed, Apr 18, 2012 at 3:13 PM, Mark Bennett <mark.f....@gmail.com> wrote:
Good to know, thanks Bob.

I went ahead and added a feature request to Uri to parse query params
into a Map.

http://code.google.com/p/dart/issues/detail?id=2645

Is there a process for contributing a patch to add this myself?


If you run into any trouble going through the process, please let us know.
Swell!

- bob

fils

unread,
Apr 18, 2012, 8:14:55 PM4/18/12
to General Dart Discussion
Thanks.. however, I think the IO library is only for the server side
VM.. (correct?)

I was actually talking about parsing the URL on the client side
VM.

This server side, client difference is going to be a bit confusing I
think to people.

Bob Nystrom

unread,
Apr 18, 2012, 8:35:46 PM4/18/12
to fils, General Dart Discussion
I believe dart:uri should be available on all Dart implementations. If you try it and it doesn't work, let me know and I'll try to get that sorted out.

- bob

fils

unread,
Apr 23, 2012, 10:52:58 AM4/23/12
to General Dart Discussion
Mark,
thanks.. this worked well..

I got the query string with
String query = document.window.location.hash;

or use ...location.search for ? URL patterns.

The rest of your code was perfect and handles the URL: /
unstructured.html#foo=bar1,bar2&this=that&empty=

as I would expect for multiple elements and empty queries too.

Thanks
Doug


On Apr 18, 4:03 pm, Mark Bennett <mark.f.benn...@gmail.com> wrote:
> I'm a newb here to so please correct me if I'm wrong, but if you're
> running the HttpServer in the Dart VM then the
> HttpRequest#queryParameters is a Map you can access.
>
> http://api.dartlang.org/io/HttpRequest.html#get:queryParameters
>
> Otherwise, if you've just got access to thestringyou could do
> something like this:
>
>  Stringquery= "foo=bar&x=y";
>
>   // Split thequeryinto pieces
>   List pieces =query.split("&").map((e) => e.split("="));
>
>   // Convert the pieces into a map
>   Map params = {};
>   pieces.forEach((piece) => params[piece[0]] = piece[1]);
>
>   print(params);
>
> Hope that helps. Curious to see if anyone knows a better way? Perhaps
> Dart needs a way to initialize a Map from a set of key / value pairs
> like in other languages?
>
> On Apr 18, 9:39 am, fils <drf...@gmail.com> wrote:
>
>
>
>
>
>
>
> > So I feel like a total noob, but for the life of me I can't find this.
>
> > Is there a simple/default  way to get a map ofqueryvalues in
Reply all
Reply to author
Forward
0 new messages