2

I am trying to get a simple ruby script to send requests to a SOAP API but I am not able to get responses back.

This is what I am trying to do:

require 'date'
require 'savon'

# Create the client
client = Savon::Client.new do
  wsdl.document = File.expand_path("path to wsdl document", __FILE__)
end

# Setup namespaces and credentials
client.wsdl.namespace = "http://www.example.com"
client.wsse.credentials "[USERNAME]", "[PASSWORD]"

# Setup ssl configuration
client.http.auth.ssl.cert_key_file = "path to key pem file"
client.http.auth.ssl.cert_file = "path to cert pem file"
client.http.auth.ssl.verify_mode=:peer

# execute request
response = client.request :sub, :get_data do
  soap.body = {"sub:id" => "123456"}
end

This request finishes with:

D, [2011-05-05T10:21:45.014588 #22136] DEBUG -- : SOAP request: "http://www.example.com"
D, [2011-05-05T10:21:45.014743 #22136] DEBUG -- : Content-Type: text/xml;charset=UTF-8, SOAPAction: "getData"
D, [2011-05-05T10:21:45.014787 #22136] DEBUG -- : <?xml version="1.0" encoding="UTF-8"?><env:Envelope  ...(XML request)...  </env:Body></env:Envelope>
D, [2011-05-05T10:21:45.014864 #22136] DEBUG -- : HTTPI executes HTTP POST using the httpclient adapter
HTTPClient::ConnectTimeoutError: execution expired

However, when I try to send the same request via curl, it works (copying the xml request above to the soap-request.xml file):

curl -k -v --header "Content-Type: text/xml;charset=UTF-8, SOAPAction: 'getData'" https://www.example.com -d@soap-request.xml --cert-type PEM --cert path_to_pem_file_with_both_key_and_cert

Any ideas about what I'm missing in the ruby script?

Thanks in advance.

UPDATE:

The code above works if the WSDL document is correct. However, in case there isn't one or in case it is erroneous, just replace the client declaration with this:

# Create the client
client = Savon::Client.new do
  wsdl.endpoint = "https://whateverendpoint.com"
  wsdl.namespace = "http://whatevernamespace.com"
end

Finally, it is also a good idea to catch possible faults as described in Savon's documentation:

begin
  # execute request
  response = client.request :sub, :get_data do
    soap.body = {"sub:id" => "123456"}
  end
rescue Savon::SOAP::Fault => fault
puts fault.to_s
end
carvil
  • 199
  • 4
  • 12

1 Answers1

1

Have you tried to extend the http timeout? I had the same problem with some of my SOAP call that took very long on the server side. What I did was this

jira = Savon::Client.new do
  wsdl.document = 'http://jira.xxx.com/rpc/soap/jirasoapservice-v2?wsdl'
end

jira.http.read_timeout = 300

done = 0
dotPrinter = Thread.new do
  sec = 0
    while(done==0) do
      sleep 1
      $STDERR.print "\b\b\b%03i" % sec
      sec += 1
    end
end

resp = jira.request :get_issues_from_filter do
  soap.body = {:in0 => jira_token, :in1 => 18579}
end
done = 1
Steffen Roller
  • 3,464
  • 25
  • 43
  • I have tried to extend the http timeout, but that didn't work. However, I read on the web that the problem could be in the wsdl document and in fact it was. I was given an erroneous document. I just removed the wsdl document declaration and added the wsdl endpoint and namespace to the client declaration and it works now. Thanks anyway for your comment. I will add the fix above. – carvil May 09 '11 at 08:46