2017-06-28 39 views
0

當調用API以檢索標籤時,我對使用ruby設置SOAP調用的難度感到沮喪。無法在使用Savon的SOAP調用中指定標頭

我知道,從一個沙箱環​​境中的以下作品:

這是應該被稱之爲

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header> 
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://postnl.nl/cif/services/BarcodeWebService/IBarcodeWebService/GenerateBarcode</Action> 
    <Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
     <wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
     <wsse:Username>devc_!R4xc8p9</wsse:Username> 
     <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">098fd559930983af31ef6630a0bb0c1974156561</wsse:Password> 
     </wsse:UsernameToken> 
    </Security> 
    </s:Header> 
    <s:Body> 
    <GenerateBarcode xmlns:d6p1="http://postnl.nl/cif/domain/BarcodeWebService/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://postnl.nl/cif/services/BarcodeWebService/"> 
     <d6p1:Message> 
     <d6p1:MessageID>5</d6p1:MessageID> 
     <d6p1:MessageTimeStamp>28-06-2017 14:15:41</d6p1:MessageTimeStamp> 
     </d6p1:Message> 
     <d6p1:Customer> 
     <d6p1:CustomerCode>DEVC</d6p1:CustomerCode> 
     <d6p1:CustomerNumber>11223344</d6p1:CustomerNumber> 
     </d6p1:Customer> 
     <d6p1:Barcode> 
     <d6p1:Type>3S</d6p1:Type> 
     <d6p1:Range>DEVC</d6p1:Range> 
     <d6p1:Serie>1000000-2000000</d6p1:Serie> 
     </d6p1:Barcode> 
    </GenerateBarcode> 
    </s:Body> 
</s:Envelope> 

編輯

由於sugested我以前build_request檢查如何我請求看起來。它實際上看起來像這樣:

<?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:wsdl="http://tempuri.org/" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> 
    <env:Header> 
    <Username>devc_!R4xc8p9</Username 
    <Password>098fd559930983af31ef6630a0bb0c1974156561</Password> 
    </env:Header> 
    <env:Body><wsdl:GenerateBarcode><CustomerCode>DEVC</CustomerCode> 
    <CustomerNumber>11223344</CustomerNumber> 
    <Type>3S</Type> 
    <Range>DEVC</Range> 
    <Serie>1000000-2000000</Serie> 
</wsdl:GenerateBarcode> 
</env:Body> 
</env:Envelope> 

與上面的xml比較,它變得清楚,東西關閉。如何處理嵌套?

這是當我建立我自己喜歡的:

@client = Savon.client(
       :convert_request_keys_to => :camelcase, 
       :raise_errors => false, 
       :pretty_print_xml => true, 
       :wsdl => 'https://testservice.postnl.com/CIF_SB/BarcodeWebService/1_1/?wsdl', 
       :headers => { 
        'Username' => 'devc_!R4xc8p9', 
        'Password' => '098fd559930983af31ef6630a0bb0c1974156561' 
       }) 

     @response = @client.call(:generate_barcode) do 
     soap_header "Username" => 'devc_!R4xc8p9', 
        "Password" => '098fd559930983af31ef6630a0bb0c1974156561' 
     message customer_code: 'DEVC', 
       customer_number: 11223344, 
       type:   '3S', 
       range:   'DEVC', 
       serie:   '1000000-2000000' 
     end 

我知道我的連接工作,同時也找回:

{:fault=>{:faultcode=>"s:CIF Framework Message Interceptor", 
    :faultstring=>"Check CIFException in the detail section", :detail=> 
    {:cif_exception=>{:errors=>{:exception_data=>{:description=>nil, 
    :error_msg=>"Username/password is not specified.", 
    :error_number=>"3"}}, 
    :@xmlns=>"http://postnl.nl/cif/services/common/", 
    :"@xmlns:i"=>"http://www.w3.org/2001/XMLSchema-instance"}}}} 

所以已經在我的頭的東西出了問題。未指定用戶名/密碼。我嘗試過幾種組合,但我不明白爲什麼。

更新

@client = Savon.client(
     :env_namespace => :s, 
     # :namespace_identifier => :none, # :none doesn't work... gets interpreted literary. Out-commeting gives wsdl:GenerateBarcode 
     :convert_request_keys_to => :camelcase, 
     :raise_errors => false, 
     :pretty_print_xml => true, 
     :endpoint => 'https://testservice.postnl.com/CIF_SB/BarcodeWebService/1_1/BarcodeWebService.svc', 
     :wsdl => 'https://testservice.postnl.com/CIF_SB/BarcodeWebService/1_1/?wsdl') 

     soap_header = { 
         "Security" => { 
         "UsernameToken" => { 
          "Username" => 'devc_!R4xc8p9', 
          "Password" => '098fd559930983af31ef6630a0bb0c1974156561' 
         } 
         } 
        } 


     message = { 
        "d6p1:Customer" => { 
         "d6p1:CustomerCode" => 'DEVC', 
         "d6p1:CustomerNumber" => 11223344 
        }, 
        "d6p1:Barcode" => { 
         "d6p1:Type" => '3S', 
         "d6p1:Range" => 'DEVC', 
         "d6p1:Serie" => '1000000-2000000' 
        } 
       } 



     @request = @client.build_request(:generate_barcode) do 
     soap_header soap_header 
     message message 
     end 

     @response = @client.call(:generate_barcode) do 
     soap_header soap_header 
     message message 

它建立以下請求:

<?xml version="1.0" encoding="UTF-8"?> 
    <s:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:s="http://tempuri.org/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header> 
    <Security> 
     <UsernameToken> 
     <Username>devc_!R4xc8p9</Username 
     <Password>098fd559930983af31ef6630a0bb0c1974156561</Password> 
     </UsernameToken> 
    </Security> 
    </s:Header> 
    <s:Body> 
    <wsdl:GenerateBarcode> 
     <d6p1:Customer> 
     <d6p1:CustomerCode>DEVC</d6p1:CustomerCode 
     <d6p1:CustomerNumber>11223344</d6p1:CustomerNumber> 
     </d6p1:Customer> 
     <d6p1:Barcode> 
     <d6p1:Type>3S</d6p1:Type> 
     <d6p1:Range>DEVC</d6p1:Range> 
     <d6p1:Serie>1000000-2000000</d6p1:Serie> 
     </d6p1:Barcode></s:GenerateBarcode> 
</s:Body> 

對於作爲然而,我得到的錯誤在soap_header散直添加wsse:命名空間。

EDIT 2

<?xml version="1.0" encoding="UTF-8"?> 
<s:Envelope 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:wsdl="http://tempuri.org/" 
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header> 
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://postnl.nl/cif/services/BarcodeWebService/IBarcodeWebService/GenerateBarcode 
    </Action> 
    <Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
     <UsernameToken> 
     <Username>devc_!R4xc8p9</Username> 
     <Password>xxxxxxxx</Password> 
     </UsernameToken> 
    </Security> 
    </s:Header> 
    <s:Body> 
    <wsdl:GenerateBarcode> 
     <d6p1:Customer> 
     <d6p1:CustomerCode>DEVC</d6p1:CustomerCode> 
     <d6p1:CustomerNumber>11223344</d6p1:CustomerNumber> 
     </d6p1:Customer> 
     <d6p1:Barcode> 
     <d6p1:Type>3S</d6p1:Type> 
     <d6p1:Range>DEVC</d6p1:Range> 
     <d6p1:Serie>1000000-2000000</d6p1:Serie> 
     </d6p1:Barcode> 
    </wsdl:GenerateBarcode> 
    </s:Body> 
</s:Envelope> 

比較這我應該調用看來我<s:envelope ...>標籤有太多的信息。

我會就此做一個跟進問題。

編輯3

我在Complex SOAP call in Ruby on Rails using Savon gets weird around the envelope and main operation

+1

嘗試在控制檯中使用此代碼調用'build_request'而不是'call',它應該輸出請求而不是嘗試發出請求,並且可以幫助查看實際發生的情況。 –

+0

謝謝,添加它,正確的下應該是XML。我似乎有困難與我的屬性嵌套。我能不能把這個xml調用保存在某處並使用'#{}'插值來添加變量?比再次解決這個問題要容易得多。 –

+0

你的用戶/密碼不應該在WSSE命名空間中嗎? –

回答

1

消息散列重新啓動,應該被嵌套在相同的方式調用希望它像

@client = Savon.client(
    :convert_request_keys_to => :camelcase, 
    :raise_errors => false, 
    :pretty_print_xml => true, 
    :wsdl => 'https://testservice.postnl.com/CIF_SB/BarcodeWebService/1_1/?wsdl', 
    :headers => { 
    'Username' => 'devc_!R4xc8p9', 
    'Password' => '098fd559930983af31ef6630a0bb0c1974156561' 
}) 

@response = @client.call(:generate_barcode) do 
    soap_header "Security" => { 
       "UsernameToken" => { 
        "Username" => 'devc_!R4xc8p9', 
        "Password" => '098fd559930983af31ef6630a0bb0c1974156561' 
       } 
       } 
    message "GenerateBarcode" => { 
      "Message" => { 
       "MessageID" => 5, #might be different 
       "MessageTime" => Time.now, #might be different depending on use case 
      }, 
      "Customer" => { 
       "CustomerCode" => 'DEVC', 
       "CustomerNumber" => 11223344 
      }, 
      "Barcode" => { 
       "Type" => '3S', 
       "Range" => 'DEVC', 
       "Serie" => '1000000-2000000' 
      } 
      } 
    end 

編輯

參考this page看起來你可以添加動作標籤和元素這樣

'Action' => "http://postnl.nl/cif/services/BarcodeWebService/IBarcodeWebService/GenerateBarcode", 
:attributes! => { 
    "s:mustUnderstand" => "1", 
    "xmlns" => "http://schemas.microsoft.com/ws/2005/05/addressing/none" 
} 

引用this page看起來你可以直接設置命名空間「的wsse:UsernameToken的」,而不是「用戶名令牌」

這將導致以下

@client = Savon.client(
    :convert_request_keys_to => :camelcase, 
    :raise_errors => false, 
    :pretty_print_xml => true, 
    :wsdl => 'https://testservice.postnl.com/CIF_SB/BarcodeWebService/1_1/?wsdl', 
    :headers => { 
    'Username' => 'devc_!R4xc8p9', 
    'Password' => '098fd559930983af31ef6630a0bb0c1974156561' 
}) 

@response = @client.call(:generate_barcode) do 
    soap_header "Security" => { 
       "wsse:UsernameToken" => { 
        "wsse:Username" => 'devc_!R4xc8p9', 
        "wsse:Password" => '098fd559930983af31ef6630a0bb0c1974156561' 
       }  
       "Action" => "http://postnl.nl/cif/services/BarcodeWebService/IBarcodeWebService/GenerateBarcode", 
       :attributes! => { 
       "s:mustUnderstand" => "1", 
       "xmlns" => "http://schemas.microsoft.com/ws/2005/05/addressing/none" 
       } 
      } 
    message "GenerateBarcode" => { 
      "Message" => { 
       "MessageID" => 5, #might be different 
       "MessageTime" => Time.now, #might be different depending on use case 
      }, 
      "Customer" => { 
       "CustomerCode" => 'DEVC', 
       "CustomerNumber" => 11223344 
      }, 
      "Barcode" => { 
       "Type" => '3S', 
       "Range" => 'DEVC', 
       "Serie" => '1000000-2000000' 
      } 
      } 
    end 
+0

幾乎只有我需要修復的名稱間距。它更像是它。 –

+0

您可能會知道如何解決的三件事:我如何在Envelope,Header和Body前添加':'。如何添加此Action標籤及其屬性和其他屬性。最後,爲什麼用'wsse:'前綴會給我一個錯誤。我會添加我的紅寶石。 –

+0

我添加了如何爲wsse組件命名空間並添加操作標記,但我不確定如何命名空間'envelope','header'和'body'沒有名稱間隔其餘 –

相關問題