2017-07-20 90 views
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> 

回答

0

你使用哪種版本薩翁?

它看起來像你錯過了在頭的一些屬性:

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

還提供一個屬性

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

這是不是在你的模板中的有效載荷。

這是你可以做的最簡單的電話嗎?沒有更簡單的東西嗎?

+0

我正在使用savon 2.11.1。這是我可以對API進行的最簡單的調用,問題可能是身體的問題,因爲我可以用ns3:security block設置整個標頭,但我不確定是否有更容易的方法標題作爲我需要的請求。 – Gabriel

+0

我認爲你缺少身體元素中的類型標籤。 –