Creating Custom SOAP Requests with Ruby and Net::HTTP

By Eric Berry
December 19, 2008 | Comments: 6

I interviewed at a company a while ago and one of the questions they asked me was the familiar "You have 8 golf balls and a justice scale. One of the golf balls has a bubble in it making it a tiny bit lighter, but you can't tell by looking at it or holding it. How many times do you need to use the scale in order to find the defective ball?". I left the interview thinking that the answer was 3. After about 3 hours of thinking about it, I realized that the answer was 2 tries. golf_scale.jpg

The reason I share this with you is because today I found the answer to something that I've been thinking about for much longer than 3 hours. For about 1 1/2 years, I have been trying to find a good way to simply post a SOAP request via ruby without having to use SOAP4r or WSDL's. Some of you might find this a bit odd that it took me this long to figure this out, but then again, it's almost impossible to find any examples of this online.

Enough history. Let's get to the code.

In my book, Rails Pocket Reference, I have a section about using SOAP. In the example, I used the soap/wsdlDriver library to parse a WSDL from GeoCoder.us, a free US address geocoder tool. Instead of using the WSDL, you can access the response directly.

I hope this makes using SOAP with Ruby a lot less cryptic for other as it does me.


You might also be interested in:

6 Comments

Great stuff...I'm working through something similar today! WSDL's are not fun on Ruby.

Thanks for your good job. It helps me a lot!!

Now I can login the user but for the next request I need to send also the cookie session so I'm trying to send the header Cookie with the cookie value I had before (set-cookie = JSESSIONID=89A34E7C1A2D944CBF89C39F3604C24C; Path=/WS; Secure):

headers = {
'Content-Type' => 'text/xml',
'Cookie' => resp["set-cookie"],
}

But this is not working... do you have any idea why?

Also the http.use_ssl = true is not working for me:
warning: peer certificate won't be verified in this SSL session
c:/ruby/lib/ruby/1.8/net/http.rb:590:in `connect': unknown protocol (OpenSSL::SSL::SSLError)
from c:/ruby/lib/ruby/1.8/net/http.rb:590:in `connect'
from c:/ruby/lib/ruby/1.8/net/http.rb:557:in `do_start'
from c:/ruby/lib/ruby/1.8/net/http.rb:546:in `start'
from c:/ruby/lib/ruby/1.8/net/http.rb:1044:in `request'
from c:/ruby/lib/ruby/1.8/net/http.rb:854:in `post'
from soap5.rb:34


I hope somebody can help me... :)

Thanks for your time.

I have what should be a really easy question, but it seems like searching on the internet for an answer is useless. I am trying to do something very similar where I have the SOAP envelope format, and there isn't a public WSDL so I want to build my own envelope. This is where I'm stuck. Right now I am behind a corporate firewall and the service that I'm trying to consume is on the other side. So I need to route my request through my company's http proxy. I think that I can somehow use Net::HTTP to create an HTTP object and tell it my proxy information, but I am not sure where to begin on this. I'm thinking I can create a Net::HTTP::Proxy object, but wonder if there's an easier way. Has anyone had to do something similar?

Wow! It never seizes to amaze me how simple Ruby code is. Thanks for the example Eric!

Hi Eric,

Thanks a lot for this post. I used all Gems but it really was a problem with the WSDL wanted to use. Always i got "Array index out of the bounds" - Direct HTTP call was also failing with same error. Your post helped me a lot. I found it after 3 days of long research.

Thanks again...
DIPAK

News Topics

Recommended for You

Got a Question?