尝试使用Savon 2构建正确的SOAP请求主体

时间:2017-07-20 18:33:49

标签: soap savon

我需要使用savon生成以下SOAP请求到外部API,这是我得到的成功请求的示例:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope
xmlns:env="http://www.w3.org/2003/05/soap-envelope"
xmlns:ns1="urn:ConsultarSucursales"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:ns2="http://xml.apache.org/xml-soap"
xmlns:ns3="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:enc="http://www.w3.org/2003/05/soap-encoding">
<env:Header>
    <ns3:Security env:mustUnderstand="true">
        <ns3:UsernameToken>
            <ns3:Username>XXXX</ns3:Username>
            <ns3:Password>XXXX</ns3:Password>
        </ns3:UsernameToken>
    </ns3:Security>
</env:Header>
<env:Body>
    <ns1:ConsultarSucursales env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
        <Consulta xsi:type="ns2:Map">
            <item>
                <key xsi:type="xsd:string">consulta</key>
                <value xsi:type="ns2:Map">
                    <item>
                        <key xsi:type="xsd:string">Localidad</key>
                        <value xsi:type="xsd:string"></value>
                    </item>
                    <item>
                        <key xsi:type="xsd:string">CodigoPostal</key>
                        <value xsi:type="xsd:string">1406</value>
                    </item>
                    <item>
                        <key xsi:type="xsd:string">Provincia</key>
                        <value xsi:type="xsd:string"></value>
                    </item>
                </value>
            </item>
        </Consulta>
    </ns1:ConsultarSucursales>
</env:Body>

使用Savon我使用了以下代码:

require 'savon'

client = Savon.client do
  wsdl 'https://sucursales.andreani.com/ws?wsdl'
  wsse_auth('XXXXX', 'XXXXX')
  convert_request_keys_to :camelcase
  soap_version 2
  env_namespace :soapenv
  namespace_identifier :ser
  ssl_verify_mode :none
  log true
end

response = client.call :consultar_sucursales, :message => {:consulta => {:codigo_postal => '1406', 'Localidad': nil, 'Provincia': nil}}

生成以下SOAP原始请求格式:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ser="urn:ConsultarSucursales"
xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Header>
    <wsse:Security
        xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
        <wsse:UsernameToken wsu:Id="UsernameToken-1"
            xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:Username>XXXXX</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXX</wsse:Password>
        </wsse:UsernameToken>
    </wsse:Security>
</soapenv:Header>
<soapenv:Body>
    <ser:ConsultarSucursales>
        <Consulta>
            <CodigoPostal>1406</CodigoPostal>
            <Localidad xsi:nil="true"/>
            <Provincia xsi:nil="true"/>
        </Consulta>
    </ser:ConsultarSucursales>
</soapenv:Body>

此请求暂时产生500错误。

我对如何生成正确的请求感到有点迷失。应该在请求中手动设置命名空间吗?我在使用Savon生成的请求中遗漏了其他内容吗?

问题更新:

在修改了一个位之后,我设法获得了与API服务器中预期相同的请求标头,但是当我将其发送到服务器时仍有500个问题。

我现在使用Savon的请求代码如下

namespaces = {
"xmlns:ns1" =>"urn:ConsultarSucursales",
"xmlns:xsi" =>"http://www.w3.org/2001/XMLSchema-instance",
"xmlns:xsd" =>"http://www.w3.org/2001/XMLSchema",
"xmlns:ns2" =>"http://xml.apache.org/xml-soap",
"xmlns:ns3" =>"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
"xmlns:enc" =>"http://www.w3.org/2003/05/soap-encoding"
}

h = %{<ns3:Security env:mustUnderstand="true"><ns3:UsernameToken><ns3:Username>XXXXX</ns3:Username><ns3:Password>XXXX</ns3:Password></ns3:UsernameToken></ns3:Security>}

client = Savon.client do
  wsdl 'https://sucursales.andreani.com/ws?wsdl'
  convert_request_keys_to :camelcase
  soap_version 2
  namespaces namespaces
  env_namespace :env
  log true
  soap_header h
end

生成此SOAP请求:

<?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:tns="urn:ConsultarSucursales"
xmlns:env="http://www.w3.org/2003/05/soap-envelope"
xmlns:ns1="urn:ConsultarSucursales"
xmlns:ns2="http://xml.apache.org/xml-soap"
xmlns:ns3="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:enc="http://www.w3.org/2003/05/soap-encoding">
<env:Header>
    <ns3:Security env:mustUnderstand="true">
        <ns3:UsernameToken>
            <ns3:Username>XXXXXX</ns3:Username>
            <ns3:Password>XXXXXX</ns3:Password>
        </ns3:UsernameToken>
    </ns3:Security>
</env:Header>
<env:Body>
    <tns:ConsultarSucursales>
        <Consulta>
            <CodigoPostal>1406</CodigoPostal>
            <Localidad xsi:nil="true"/>
            <Provincia xsi:nil="true"/>
        </Consulta>
    </tns:ConsultarSucursales>
</env:Body>
</env:Envelope>

1 个答案:

答案 0 :(得分:0)

你使用哪个Savon版本?

看起来你错过了标题中的一些属性:

<ns3:Security env:mustUnderstand="true">

您还提供了一个属性

<wsse:UsernameToken wsu:Id="UsernameToken-1"

不在您的模板有效负载中。

这是你可以做的最简单的电话吗?难道还有更简单的东西吗?