Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot post duplicate values (arrays) with agent.post #132

Closed
jg opened this issue Aug 2, 2011 · 7 comments
Closed

Cannot post duplicate values (arrays) with agent.post #132

jg opened this issue Aug 2, 2011 · 7 comments

Comments

@jg
Copy link

jg commented Aug 2, 2011

Attempt (doesn't work):

page = agent.post(url, {"array[]"=> ["element1", "element2"]})

The equivalent curl invocation (works):

curl -d url "array[]=element1&array[]=element2"

@jg jg closed this as completed Sep 3, 2011
@sguha00
Copy link

sguha00 commented Sep 18, 2013

What's the correct way to post an array with Mechanize?

@knu
Copy link
Member

knu commented Sep 18, 2013

Mechanize does not currently grok multiple values in an array.

Instead, request methods take an array of pairs as parameters, so you could do as follows:

page = agent.post(url, ["element1", "element2"].map { |v| ["array[]", v] })

If you have other parameters:

page = agent.post(url, [[:x, 1], [:y, 2], *["element1", "element2"].map { |v| ["array[]", v] }])

I'll be working on improving this situation when I get the time.

@knu knu reopened this Sep 18, 2013
@knu
Copy link
Member

knu commented Sep 18, 2013

It seems pretty popular to be able to pass a hash also, like array[]=element1&array[]=element2&hash[key1]=value1&hash[key2]=value2, so I'll consider making Mechanize support that too, while making it optional to encode brackets.

@sguha00
Copy link

sguha00 commented Sep 18, 2013

Thanks, the array of pairs way worked for me

@knu
Copy link
Member

knu commented Sep 24, 2013

It seems PHP's http_build_query() is not very compatible with how an array of values assigned to one key is encoded by other well-known implementations of perl, ruby and python.

% php -r 'echo http_build_query(array("foo" => array("x","y"))) . "\n";'
foo%5B0%5D=x&foo%5B1%5D=y
% ruby -ruri -e 'puts URI.encode_www_form("foo" => ["x","y"])'
foo=x&foo=y
% perl -MURI -le '$u = URI->new("http://host/"); $u->query_form(foo => ["x","y"]); print $u->query'
foo=x&foo=y
% python -c 'import urllib; print urllib.urlencode({"foo": ["x","y"]}, True)'
foo=x&foo=y

(And it seems fine to encode brackets)

So, what I have in mind now is:

  • { "foo" => ["x","y"] } becomes foo=x&foo=y
  • { "foo[]" => ["x","y"] } becomes foo%5B%5D=x&foo%5B%5D=y
  • { "foo" => { "x"=>"a", "y"=>"b" } } becomes foo%5Bx%5D=a&foo%5By%5D=b

and maybe

  • { "foo" => ["x","y"].each_with_index } becomes foo%5B0%5D=x&foo%5B1%5D=y

@knu
Copy link
Member

knu commented Sep 24, 2013

Closing this as the original problem is solved.
I'll be working on the new feature when I get the time.

@knu knu closed this as completed Sep 24, 2013
@knu
Copy link
Member

knu commented Feb 26, 2014

I've just implemented array/hash value support for query/post parameters.
Check out v2.7.4.beta2 or later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants