Savon返回NoMethodError错误

时间:2018-01-03 21:36:52

标签: ruby soap savon

我是Savon的新手,获得configure_headers': undefined method%' for nil:尝试发送soap请求时出现NilClass(NoMethodError)错误 我的代码看起来像这样

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' })

通过SoapUI请求看起来像这样

<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>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

如果你看一下section of source code that the stack trace points to,错误的原因很容易解决:

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]

错误源于您在客户端设置soap_version "2" String )。相反,正如您从上面的哈希中所看到的,它需要是 Integer

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

图书馆的这个微妙的事实is documented