How to deal with duplicate query params as an array?

271 views
Skip to first unread message

Andrew Havens

unread,
May 15, 2014, 2:07:19 PM5/15/14
to httpar...@googlegroups.com
I'm working with an API which accepts duplicate params as an array. For example:

/search.json?people=joe&people=jane

I would have expected them to handle this array differently (comma separated, or with square brackets), but I'm stuck with having to comply with their decision. However, the problem I'm running into is that Ruby doesn't accept duplicate hash keys: 

MyClient.get('/search.json', people: 'joe', people: 'jane')

What are my options? How can I handle this requirement without hard-coding the URL?

John Nunemaker

unread,
May 15, 2014, 3:17:59 PM5/15/14
to httpar...@googlegroups.com
$ irb
irb(main):001:0> require "uri"
=> true
irb(main):002:0> URI.encode_www_form("people" => ['john', 'joe'])
=> "people=john&people=joe”

Therefore you could do:

MyClient.get(‘/search.json?’ + URI.encode_www_form(“people” => [‘john’, ‘joe’] ))
--
You received this message because you are subscribed to the Google Groups "HTTParty Gem" group.
To unsubscribe from this group and stop receiving emails from it, send an email to httparty-gem...@googlegroups.com.
To post to this group, send email to httpar...@googlegroups.com.
Visit this group at http://groups.google.com/group/httparty-gem.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages