2011-11-19 112 views
0

如何使用Savon將編碼屬性添加到body標籤?Savon:將編碼添加到body標籤

一些背景: 我想用savon連接到一個SOAP資源。我可以獲取WSDL文件並瀏覽這些方法。

@client = Savon::Client.new("http://some.domain.com/v2messaging/service?WSDL") 

當我嘗試使用的登錄方法

response = @client.request :service, :login do 
    soap.body = { 
    "String_1" => "username", 
    "String_2" => "password" 
    } 
end 

我得到這個錯誤:

故障/錯誤:響應= @ client.request:服務:登錄做薩翁: :SOAP :: Fault:(env:Client)在處理請求時捕獲到異常:意外的編碼風格:expected = http://schemas.xmlsoap.org/soap/encoding/,實際

身體標記的區別。這裏是預期的XML(通過SOAPUI應用程序中找到):

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:service="etapestryAPI/service"> 
    <env:header/> 
    <env:body> 
     <service:login env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
     <String_1>username</String_1> 
     <String_2>password</String_2> 
     </service:login> 
    </env:body> 
</env:Envelope> 

薩翁發送:

<?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:service="etapestryAPI/service" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ins0="http://java.sun.com/jax-rpc-ri/internal" xmlns:ins1="etapestryAPI/service"> 
    <env:Body> 
     <service:login> 
      <String_1>username</String_1> 
      <String_2>password</String_2> 
     </service:login> 
    </env:Body> 
</env:Envelope> 

有這之間的一些差異,但錯誤返回已與ENV做:encodingStyle屬性在env:login標籤上。如何添加此屬性?

回答

1

我想出了這一個。爲了增加功能標籤(在這種情況下登錄)的屬性,你可以在一個額外的參數傳遞給方法:

response = @client.request :service, :login, "env:encodingStyle" => "http://schemas.xmlsoap.org/soap/encoding/" do 
    soap.body = { 
      "String_1" => "username", 
      "String_2" => "password" 
     } 
end 

這將可能現在不經過塊工作。