Savon: SOAP in Ruby

Alena Kruchkova
Tripping Engineering
3 min readDec 19, 2016

--

There comes a time in every developer’s life when she must go on a magical unicorn ride that is an implementation of SOAP API.

What is SOAP?

SOAP is an acronym for Simple Object Access Protocol, but this standards-based Web services access protocol is anything but simple.

SOAP APIs primarily use XML, where each input parameter is defined and bound to a type, to make requests and receive responses.

Here is an example of a typical SOAP request:

And a typical SOAP response:

How to consume SOAP in Ruby?

If that is the task at hand Savon is definitely a right tool to use! Here is how:

  1. Install the Gem
  2. Create a client:
*Anatomy of wsdl url: http://<Host_from_SOAP_request>/<request_from_SOAP_request>?WSDL

3. Make a request:

4. Response has a header and a body method. Calling response.body will return a hash:

Editing SOAP message to use newer Ruby syntax for hashes

When new Ruby hash syntax is being used in SOAP message, Savon tells Gyoku to convert key Symbols to lowerCamelcase tags. You can change this to CamelCase, UPCASE or completely disable any conversion.

*for our example disabling conversion is the best option

Nested parameters in SOAP request

What if you are trying to implement a SOAP request that looks like this?

Just put all of your nested arguments in an array:

Using proxies with Savon

Using proxies when making SOAP requests via Savon is as easy as adding this line to your client:

proxy: ‘https://<proxy_address>:<port>’

The client will now look like this:

Debugging

Add the following arguments to your client for complete log:

And that’s it! I hope you enjoyed this magical journey as much as I did. SOAP implementations may not be every Ruby developer’s cup of tea, but with the right tools, you can conquer these APIs with ease. Good luck!

--

--