Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

consume soap service with ruby and savon

I'm trying to use ruby and Savon to consume a web service.

The test service is http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2

require 'rubygems'
require 'savon'

client = Savon::Client.new "http://www.webservicex.net/stockquote.asmx?WSDL"
client.get_quote do |soap| 
  soap.body = {:symbol => "AAPL"} 
end

Which returns an SOAP exception. Inspecting the soap envelope, it looks to me that the soap request doesn't have the correct namespace(s).

Can anyone suggest what I can do to make this work? I have the same problem with other web service endpoints as well.

Thanks,

like image 967
tom Avatar asked Aug 10 '10 20:08

tom


2 Answers

This is a problem with the way Savon handles Namespaces. See this answer Why is "wsdl" namespace interjected into action name when using savon for ruby soap communication?

You can resolve this by specifically calling soap.input and passing it an array, the first element is the method and the second is a hash containing the namespace(s)

require 'rubygems'
require 'savon'

client = Savon::Client.new "http://www.webservicex.net/stockquote.asmx?WSDL"
client.get_quote do |soap| 
  soap.input = [ 
    "GetQuote", 
    { "xmlns" => "http://www.webserviceX.NET/" } 
  ]
  soap.body = {:symbol => "AAPL"} 
end
like image 92
Steve Weet Avatar answered Nov 05 '22 05:11

Steve Weet


You might find the latest gem uses the method "request" followed by the symbol reference to the method required.

require 'rubygems'
require 'savon'

client = Savon::Client.new "http://www.webservicex.net/stockquote.asmx?WSDL"
client.request :get_quote do |soap| 
    soap.input = [ 
    "GetQuote", 
    { "xmlns" => "http://www.webserviceX.NET/" } 
    ]
    soap.body = {:symbol => "AAPL"} 
end
like image 36
Chris Edwards Avatar answered Nov 05 '22 05:11

Chris Edwards