# I can't seem to find an easy way to set Net::HTTP GET params

**URL:** <https://www.ruby-forum.com/t/i-cant-seem-to-find-an-easy-way-to-set-net-http-get-params/179345>\
**Category:** Ruby\
**Created:** [November 23, 2009, 12:14am UTC](https://www.ruby-forum.com/t/i-cant-seem-to-find-an-easy-way-to-set-net-http-get-params/179345 "2009-11-23T00:14:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 23, 2009, 12:14am UTC](https://www.ruby-forum.com/t/i-cant-seem-to-find-an-easy-way-to-set-net-http-get-params/179345/1 "2009-11-23T00:14:47Z")

</div>

Hello,

I’m sure this is a newb question, but I’ve spent the last two hours  
trying to figure out how to set the query paramters on an HTTP GET  
request.

Right now I am doing something like this:

def call()  
Net::HTTP.start(@base\_url) do |http|  
response = http.get(@uri)  
puts “Code = #{response.code}”  
puts “Message = #{response.message}”  
puts “Body = #{response.body}”  
end

It works as long as I include the query parameters as part of the @uri  
string, but to me that doesn’t seem like an elegant approach.

I could just append the parameters to the end of the URI as part of the  
call() method (right now it has no parameters, but I could change it to  
something like call(params) which accepts a hash and manually append  
them to the URI before calling the GET method.

I’m just hoping that there’s an easier, or more standard way of doing  
it.

Anyone ever run into something similar and has an elegant solution?

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 23, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/i-cant-seem-to-find-an-easy-way-to-set-net-http-get-params/179345/2 "2009-11-23T18:31:07Z")

</div>

On Nov 22, 5:14 pm, David Sainte-claire [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hello,
> 
> I’m sure this is a newb question, but I’ve spent the last two hours  
> trying to figure out how to set the query paramters on an HTTP GET  
> request.

You have a couple options depending on what sort of input you’re  
dealing with.

uri = URI.parse(‘[hello - Google Search](http://www.google.com/search?q=hello)’)

# or: uri = URI::HTTP.build({:host =\> ‘[www.google.com](http://www.google.com)’, :path =\> '/

search’, :query =\> ‘q=hello’})

Net::HTTP.start(uri.host) do |http|  
response = http.get(uri.request\_uri)  
# …  
end

However, as far as I know, there isn’t a method that will do a GET in  
a similar way to the POST methods (i.e. you can’t pass a hash of name/  
value pairs). To make that happen, you’ll need another method. Perhaps  
something like this:

require ‘cgi’  
def querify(query\_hash)  
query\_hash.map {|name, value| “#{CGI::escape(name.to\_s)}=#  
{CGI::escape(value.to\_s)}” }.join(‘&’)  
end

Then you could do:

uri = URI::HTTP.build({:host =\> ‘[www.google.com](http://www.google.com)’, :path =\> ‘/  
search’, :query =\> querify({:q =\> ‘hello’})})

Maybe I’m wrong and there is a better way to do this, but I haven’t  
seen it in core/standard library. Anybody?

Jeremy
