8

I'm using Ruby 1.9.2 with savon 0.9.2 on Windows 7 Professional 64 bit.

I need to call a web SOAP service that requires a security token that I get from a second web SOAP service. The code I use is as follows:

require 'savon'

client = Savon::Client.new "http://some.url?wsdl"
client.wsdl.soap_actions

start_session_response = client.request :start_session do
  soap.input = ["StartSession", {:xmlns => "http://some.schema" } ]
  soap.body = { :userName => "User", :password => "password" }
end

do_something_response = client.request :do_something do
  soap.input = [ "DoSomething", { :xmlns => "http://some.schema"} ]
  soap.body = { :securityToken => start_session_response.to_hash[:start_session_response][:security_token] }
end

This results in XML that looks like:

<?xml version="1.0" encoding="utf-8"?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:wsdl="http://some.schema"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
  <env:Body>
    <DoSomething xmlns="http://some.schema">
      <wsdl:securityToken>
        <wsdl:tokenType>sessiontoken</wsdl:tokenType>
        <wsdl:token>
         .
        .
        .
        </wsdl:token>
      </wsdl:securityToken>
    </DoSomething>
  </env:Body>
</env:Envelope>

Never mind the weird namespace convention (or is that just me) in this XML that is savon doing its thing.

The problem I face is that the tags inside the securitytoken tag all start with a lower case letter where they should be upper case. So <tokenType> and <token> should have been <TokenType> and <Token>.

In my opinion the definition of these tags are all in the WSDL that is used to create the savon client. That definition seems not to be used or used incorrectly.

What can I do to get the correct XML/SOAP message from savon?

Bob Groeneveld
  • 903
  • 1
  • 9
  • 19

3 Answers3

22

For later releases of Savon, you should be able to supply a 'global' option of convert_request_keys_to when you initialize your Savon client:

# In Savon 2
Savon.client wsdl:"http://some.url?wsdl", convert_request_keys_to: :camelcase

According to comments in the source file, it accepts one of :lower_camelcase, :camelcase, :upcase, or :none.

JellicleCat
  • 28,480
  • 24
  • 109
  • 162
  • Seems that the default is to convert to lower_camelcase. Setting value to none solved my problem. – justingordon Oct 23 '14 at 01:54
  • if you need a custom converter, you can either supply a block, or simply have the keys as strings instead of symbols (strings don't get parsed) – Mirror318 Nov 21 '17 at 04:27
13

I had a similar problem with Savon and ended up using strings in stead of symbols for my hash keys, you could try something like:

soap.body = { 'TokenType'=> 'some_value', 'Token' => 'some_value' }
Cimm
  • 4,653
  • 8
  • 40
  • 66
10

Savon uses Gyoku for the conversion of tags I believe. To change the symbol conversion you can insert the following statement:

Gyoku.convert_symbols_to :camelcase # or one of [:none, :lover_camelcase]

hope that helps.

Steffen Roller
  • 3,464
  • 25
  • 43
  • 1
    This solved the problem of making the tags CamelCase, thanks. The only problem I now have is that the tag "securityToken" does need to have a lowercase letter at the start and by using the CamelCase setting savon also gives that a capital S. It's a pity that savon does not use the WSDL provided to check the XML it generates, that seems the whole purpose of supplying it – Bob Groeneveld Jun 27 '11 at 21:47
  • As I don't have enough reputation I can't vote your answer up, I will do so as soon as I have the required reputation... – Bob Groeneveld Jun 27 '11 at 21:49