Savon 返回 NoMethodError 错误

Savon returning NoMethodError error

我是 Savon 的新手,在尝试发送 soap 请求时出现 configure_headers': undefined method%' for nil: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>

非常感谢任何帮助。

如果您查看 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" (a 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 由图书馆提供。