Richlewis Richlewis - 1 year ago 126
Ruby Question

Do not URI encode when using httparty

I have a request setup using httparty and would like to not have URI encoding take place. I have read that there is a class method available

query_string_normalizer proc { |query| do |key, value| {|v| "#{key}=#{v}"}

But I am unsure of how to use this in my current setup

url = 'https://url/here/report.json?'
query = { 'param1' => '125894',
'param2' => 'yesterday',
'param3' => 'about',
'param4' => 'client',
'parameters' => 'ns_ti:*',
'user' => ENV['USERNAME'],
'password' => ENV['PASSWORD']
response = HTTParty.get(url, query: query)
results = JSON.parse(response.body)

The issue here is that
gets encoded and gets sent as 'ns_ti%3A%2A' and i would like to avoid that

If anyone can help it would be much appreciated


Answer Source

Make your own wrapper for HTTParty and pass a proc to the normalizer that overrides the URI encoding.



class RichLewisHttp

  include HTTParty

  query_string_normalizer proc { |query| do |key, value| {|v| "#{key}=#{v}"}


Then you can do...

response = RichLewisHttp.get(url, query: query)
results = JSON.parse(response.body)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download