Converting http_params to hash

ruby, ruby-on-rails, ruby-on-rails-3

Solution

require 'cgi'
hash = CGI::parse http_params

Or you can use:

hash = Rack::Utils.parse_nested_query http_params

Which does not return the values as arrays.

Problem

I can obtain an array from the string ``` http_params="created_end_date=2013-02-28&created_start_date=2013-01-01&page_size=50&offset=0&order_id=0D1108211501118%0D%0A0D11108211501118%0D%0Ac%0D%0AD%0D%0ADK212071409743%0D%0AKK30109110100%0D%0AKK30111140300%0D%0AKK30111140400%0D%0AKK30115120100%0D%0AKK30115150100&page_number=1" ``` So I did `myarray=http_params.split("&")`: ``` myarray=["created_end_date=2013-02-28", "created_start_date=2013-01-01", "page_size=50", "offset=0", "order_id=0D1108211501118%0D%0A0D11108211501118%0D%0Ac%0D%0AD%0D%0ADK212071409743%0D%0AKK30109110100%0D%0AKK30111140300%0D%0AKK30111140400%0D%0AKK30115120100%0D%0AKK30115150100", "page_number=1"] ``` I need to convert this to a hash myhash, so that I can make a Rest Client post call for myhash.to_json. Basically it should be key,value pairs like: ``` {:created_end_date=>"2013-02-28",:created_start_date=>"2013-01-01"....} ``` I know that the inverse operation can be done like this: ``` http_params = myhash.map{|k,v| "#{k}=#{v}"}.join('&') ``` but I am unable to come up with neat code for this. What's the best way I should go about this?

Original source