# Problem constructing a url with uri

**URL:** <https://www.ruby-forum.com/t/problem-constructing-a-url-with-uri/62822>\
**Category:** Ruby\
**Created:** [June 8, 2006, 1:34pm UTC](https://www.ruby-forum.com/t/problem-constructing-a-url-with-uri/62822 "2006-06-08T13:34:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [June 8, 2006, 1:34pm UTC](https://www.ruby-forum.com/t/problem-constructing-a-url-with-uri/62822/1 "2006-06-08T13:34:21Z")

</div>

I have this, example, code:

require ‘uri’

a = Hash.new  
a[“fred”] = 42  
a[“tom”] = “dick”

x = URI::HTTP.build( {:host =\> “[fred.com](http://fred.com)”, :query =\> a} )  
puts x.to\_s

Which I was expecting to create a url like  
[Sites-fredInternationalWebsite-Site](http://fred.com/?fred=42&tom=dick) but all I get is

/usr/lib/ruby/1.8/uri/generic.rb:508:in `=~': can't convert Hash into String (TypeError) from /usr/lib/ruby/1.8/uri/generic.rb:508:in `check\_query’  
from /usr/lib/ruby/1.8/uri/generic.rb:523:in `query=' from /usr/lib/ruby/1.8/uri/generic.rb:178:in `initialize’  
from /usr/lib/ruby/1.8/uri/http.rb:46:in `initialize' from /usr/lib/ruby/1.8/uri/generic.rb:125:in `new’  
from /usr/lib/ruby/1.8/uri/generic.rb:125:in `build' from /usr/lib/ruby/1.8/uri/http.rb:36:in `build’  
from x:7

Am I correct in my assumption that :query will take a hash and the  
correct arguments or do I need to create my own function to do the  
conversion?

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [June 8, 2006, 4:24pm UTC](https://www.ruby-forum.com/t/problem-constructing-a-url-with-uri/62822/2 "2006-06-08T16:24:26Z")

</div>

On Jun 8, 2006, at 7:34 AM, Peter H. wrote:

> ```
> from /usr/lib/ruby/1.8/uri/generic.rb:125:in `build'
> from /usr/lib/ruby/1.8/uri/http.rb:36:in `build'
> from x:7
> 
> ```
> 
> Am I correct in my assumption that :query will take a hash and the  
> correct arguments or do I need to create my own function to do the  
> conversion?

:query appears to have be a string. All it does afaict is tack a ?  
with the string for query on the end.

e.g.

URI::HTTP.build(:host =\> “[ruby-lang.org](http://ruby-lang.org)”, :query =\> “ruby”).to\_s  
#=\> “[Ruby Programming Language](http://ruby-lang.org?ruby)”

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [June 8, 2006, 4:36pm UTC](https://www.ruby-forum.com/t/problem-constructing-a-url-with-uri/62822/3 "2006-06-08T16:36:02Z")

</div>

From looking at the code, it seems to expect the query to be a string in  
the appropriate format.

Cheers

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [June 8, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/problem-constructing-a-url-with-uri/62822/4 "2006-06-08T16:57:10Z")

</div>

Peter H. wrote:

> a = Hash.new  
> a[“fred”] = 42  
> a[“tom”] = “dick”
> 
> x = URI::HTTP.build( {:host =\> “[fred.com](http://fred.com)”, :query =\> a} )

:query should be a string – or rather, should respond to #to\_str.

q = {:fred =\> 42, :tom =\> ‘dick’}  
def q.to\_str  
collect{|name, value| “#{name}=#{value}”}.join(‘&’)  
end  
uri = URI::HTTP.build(:host =\> “[fred.com](http://fred.com)”, :query =\> q)

Cheers,  
Daniel
