GET request with array params

2,693 views
Skip to first unread message

Eric Seidel

unread,
Apr 14, 2011, 5:45:10 PM4/14/11
to typh...@googlegroups.com
Hi, I'm playing around with Typhoeus to see if I can use it to interface with Fluidinfo. One of the most common requests for me involves passing multiple tags that I want returned as URL params, i.e. http://fluiddb.fluidinfo.com/values?query=has+gridaphobe%2Femployer&tag=gridaphobe%2Fcompany&tag=gridaphobe%2Ftitle. To accomplish this I tried the following line:

Typhoeus::Request.get("http://fluiddb.fluidinfo.com/values", :params => {:query => "has gridaphobe/employer", :tag => ["gridaphobe/company", "gridaphobe/title"]})

The problem is that Typhoeus adds "[]" (or "%5B%5D" after url-encoding), to "tag" in the URL params, so I get "&tag%5B%5D=gridaphobe%2Fcompany&tag%5B%5D=gridaphobe%2Ftitle" instead. Is there a way to prevent this?

Thanks,

Eric

David Balatero

unread,
Apr 14, 2011, 7:14:10 PM4/14/11
to typh...@googlegroups.com, Eric Seidel
Hey Eric,

Currently there is no way to prevent this from happening. I'm a little unclear on the HTTP spec for array parameters. Are both these forms accepted/common?

tags=ok&tags=yes
tags[]=ok&tags[]=yes

Thanks!
David

Eric Seidel

unread,
Apr 14, 2011, 7:18:58 PM4/14/11
to typh...@googlegroups.com, Eric Seidel
Hey David,

only the first form is accepted. I'm honestly not sure why typhoeus inserts the [].. It's smart enough to realize that the params are an array and repeat "tags" instead of just appending each item in the array like some other gems :/

Thanks,

Eric

Evan Stoner

unread,
Apr 14, 2011, 8:01:21 PM4/14/11
to typh...@googlegroups.com, Eric Seidel
I think the issue is that both are valid in HTTP, but not all web services accept both forms.

As far as I can tell, the W3C recommendation is that one key can be repeated multiple times with multiple values, with no brackets required or expected.  There's a newer (non-W3C? grassroots?) convention that signifies an array by adding brackets to the key.

Since the brackets are allowed over HTTP but don't normally carry any special meaning, some web services don't understand the newer array notation, interpret the [] as being literally part of the key string, and break.

Put another way, the bracket notation is arguably nonstandard, although obviously common.

Thanks,
Evan

--
You received this message because you are subscribed to the Google Groups "Typhoeus" group.
To post to this group, send email to typh...@googlegroups.com.
To unsubscribe from this group, send email to typhoeus+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/typhoeus?hl=en.

Eric Seidel

unread,
Apr 18, 2011, 11:52:26 AM4/18/11
to typh...@googlegroups.com, Eric Seidel
Given that the bracket notation is not part of the W3C recommendations, I would suggest to either remove it or add some kind of flag to enable/disable it. Am I correct to assume that the services that do use the bracket notation will also recognize array params without the brackets?

Eric

David Balatero

unread,
Apr 19, 2011, 2:26:07 PM4/19/11
to typh...@googlegroups.com, Eric Seidel
On Mon, Apr 18, 2011 at 8:52 AM, Eric Seidel <grida...@gmail.com> wrote:
Given that the bracket notation is not part of the W3C recommendations, I would suggest to either remove it or add some kind of flag to enable/disable it.

That would be good.
 
Am I correct to assume that the services that do use the bracket notation will also recognize array params without the brackets?

I'm not quite sure about that. If not, though, it seems like you could still do:

{ "a[]" => [1, 2, 3] }

And that would work for the services that only take brackets?
 
Eric


Eric Seidel

unread,
Apr 20, 2011, 12:29:03 PM4/20/11
to typh...@googlegroups.com, Eric Seidel
I just sent you a pull request implementing the change in behavior.

Thanks,

Eric

David Balatero

unread,
Apr 21, 2011, 1:32:59 PM4/21/11
to typh...@googlegroups.com
Ok thanks. I'm out of country so I can't merge -- can you monkey patch for now?

David

Eric Seidel

unread,
Apr 22, 2011, 12:11:01 AM4/22/11
to typh...@googlegroups.com
Yep, no problem.

Eric

Sent from my iPhone

On Apr 21, 2011, at 13:32, David Balatero <dbal...@gmail.com> wrote:

> Ok thanks. I'm out of country so I can't merge -- can you monkey patch for now?
>
> David
>

Reply all
Reply to author
Forward
0 new messages