0
我需要生成与萨翁以下SOAP请求外部API,这是一个例子,我有一个什么样的成功的请求是:试图与萨翁建立正确的SOAP请求主体2
<?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>
随着萨翁我使用以下代码:
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错误。
我对如何生成正确的请求感到有点遗憾。应该在请求中手动设置名称空间?我错过了与萨龙生成的请求的其他内容?
更新的问题:
修改位请求后,我设法具有相同的请求头,因为它是预计在API服务器,但仍然具有500问题,当我把它发送到服务器。
我与萨翁使用请求的代码现在是以下
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>
我正在使用savon 2.11.1。这是我可以对API进行的最简单的调用,问题可能是身体的问题,因为我可以用ns3:security block设置整个标头,但我不确定是否有更容易的方法标题作为我需要的请求。 – Gabriel
我认为你缺少身体元素中的类型标签。 –