正确构造这个SOAP头?

时间:2013-01-15 17:19:43

标签: ruby soap savon

我正在尝试构建一个合适的SOAP标头:

<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sen="https://webservices.averittexpress.com/SendWebImageService">
<soapenv:Header
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ns:authnHeader
soapenv:mustUnderstand="0"
xmlns:ns="http://webservices.averittexpress.com/authn">
<Username>xxxxxxxx</Username> <Password>xxxxxxxx</Password>
</ns:authnHeader> </soapenv:Header>

这是我的Savon客户端调用,我正在使用版本2:

client = Savon.client(
  wsdl: api_url,
  raise_errors: false,
  convert_request_keys_to: :camelcase,
  element_form_default: :qualified,
  env_namespace: :soapenv,
  soap_header: {'username' => username, 'password' => password }
)

我收到以下SOAP错误:

fault=>
  {:faultcode=>"soapenv:Server",
  :faultstring=>"java.lang.NullPointerException",
  :detail=>nil}}

如何获得sapoenv:mustUserstand="0"行,它是什么?

另外,我很困惑如何设置xmlns:ns="http://webservices.averittexpress.com/authn">

我几乎没有使用SOAP请求的经验,而且来自Ruby / Rails RESTful背景。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:4)

Savon使用Gyoku将SOAP标头和正文转换为XML 遵循这些库惯例,这是您的Hash需要的样子:

soap_header = {
  "ns:authnHeader" => {
    "@soapenv:mustUnderstand" => 0,
    "@xmlns:ns" => "http://webservices.averittexpress.com/authn",
    "Username"  => "x",
    "Password"  => "x"
  }
}

Savon.client(:soap_header => soap_header)