KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
In Python, I can do this: >>> import urlparse, urllib >>> q = urlparse.parse_qsl("a=b&a=c&d=e") >>> urllib.urlencode(q) 'a=b&a=c&d=e' In Ruby[+Rails] I can't figure out how to do the same thing without "rolling my own," which seems odd. The Rails way doesn't work for me -- it adds square brackets to the names of the query parameters, which the server on the other end may or may not support: >> q = CGI.parse("a=b&a=c&d=e") => {"a"=>["b", "c"], "d"=>["e"]} >> q.to_params => "a[]=b&a[]=c&d[]=e" My use case is simply that I wish to muck with the values of some of the values in the query-string portion of the URL. It seemed natural to lean on the standard library and/or Rails, and write something like this: uri = URI.parse("http://example.com/foo?a=b&a=c&d=e") q = CGI.parse(uri.query) q.delete("d") q["a"] << "d" uri.query = q.to_params # should be to_param or to_query instead? puts Net::HTTP.get_response(uri) but only if the resulting URI is in fact http://example.com/foo?a=b&a=c&a=d , and not http://example.com/foo?a[]=b&a[]=c&a[]=d . Is there a correct or better way to do this?
Tags (comma-separated)
Save Edits
Cancel