Search code examples
rubysoapsavon

Savon returning NoMethodError error


I'm new to Savon, getting configure_headers': undefined method%' for nil:NilClass (NoMethodError) error when trying to send a soap request my code looks like this

client = Savon.client do
  wsdl "http://sfds:9080/f/sca/BrokerService/WEBINF/wsdl/client/BrokerService_BrokerService.wsdl"
  soap_version "2"
end

response =  client.call(:get_broker_details, message: { brokerId: 'testbroker' })

Request through SoapUI looks like this

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
 xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-
 wssecurity-secext-1.0.xsd" 
 xmlns:xyz="http://xml.bnz.co.nz/bnzintegrationcommon/BNZServiceHeadeV120" 
 xmlns:brok="http://xml.xyz.co.nz/brokerservice/BrokerService">
   <soap:Header>
     <oas:Security>
     <!--Optional:-->
     <oas:UsernameToken>
        <oas:Username></oas:Username>
        <oas:Password></oas:Password>
     </oas:UsernameToken>
  </oas:Security>
     <x:serviceHeaderV120>
     <user>
        <channel>BKLP</channel>
        <!--Optional:-->
        <id idType="staffId">631447</id>
        <!--Optional:-->
        <subId></subId>
        <!--Optional:-->
        <location>BRCH0573</location>
     </user>
     <application>
        <id>1</id>
        <!--Optional:-->
        <correlationToken>11111</correlationToken>
     </application>
  </x:serviceHeaderV120>
</soap:Header>
 <soap:Body>
    <brok:getBrokerDetails>
       <getBrokerDetailsRequest>
         <brokerId>ZS1002</brokerId>
       </getBrokerDetailsRequest>
    </brok:getBrokerDetails>
 </soap:Body>
</soap:Envelope>

will highly appreciate any help.


Solution

  • If you look at section of source code that the stack trace points to, the reason for the error is fairly easy to work out:

    CONTENT_TYPE = {
      1 => "text/xml;charset=%s",
      2 => "application/soap+xml;charset=%s"
    }
    
    # ...
    
    # This line was the cause of the error:
    @http_request.headers["Content-Type"] ||= CONTENT_TYPE[@globals[:soap_version]] % @globals[:encoding]
    

    The error stems from the fact that you set soap_version "2" (a String) in your client. Instead, as you can see from the hash above, it needs to be an Integer:

    client = Savon.client do
      wsdl "http://sfds:9080/f/sca/BrokerService/WEBINF/wsdl/client/BrokerService_BrokerService.wsdl"
      soap_version 2 # <-- !!!
    end
    

    This subtle fact is documented by the library.