[英]Soap Request Format when using Savon in ruby
我正在處理soap api,它提供以下示例作為請求XML外觀的示例:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsg="http://tempuri.org/wsGenRateEstimate/">
<soap:Header/>
<soap:Body>
<RateEstimateRequestVO>
<Token>7e2c61c4-8b4c-4d8b-b47f-ed033c6f4307</Token>
<CustomerNumber>1</CustomerNumber>
<OriginCity>Dothan</OriginCity>
<OriginState>AL</OriginState>
<OriginZip>36303</OriginZip>
<OriginCountryCode>USA</OriginCountryCode>
<DestinationCity>Atlanta</DestinationCity>
<DestinationState>GA</DestinationState>
<DestinationZip>30303</DestinationZip>
<DestinCountryCode>USA</DestinCountryCode>
<WhoAmI>S</WhoAmI>
<BillDate>050415</BillDate>
<CODAmount></CODAmount>
<CODPayType></CODPayType>
<CODFeePaidBy></CODFeePaidBy>
<FullCoverage>Y</FullCoverage>
<FullCoverageAmount>32545</FullCoverageAmount>
<PrePaidCollect></PrePaidCollect>
<TotalPalletCount></TotalPalletCount>
<!--Zero or more repetitions:-->
<AccLine>
<AccCode></AccCode>
</AccLine>
<!--Zero or more repetitions:-->
<RateEstimateRequestLine>
<Weight>122</Weight>
<Class>70</Class>
<HandlingUnits></HandlingUnits>
<HandlingUnitType></HandlingUnitType>
<Hazmat></Hazmat>
<CubeU></CubeU>
<Length></Length>
<Height></Height>
<Width></Width>
</RateEstimateRequestLine>
</RateEstimateRequestVO>
</soap:Body>
</soap:Envelope>
下面是我用來在Rails中嘗試此請求的代碼(出於隱私的考慮而收集我的訪問令牌):
require 'savon'
client = Savon.client({ wsdl: "http://wsportal.aaacooper.com:8188/wsGenRateEstimate.wsdl" })
# this part is to check what the XML I am sending will look like
request = client.build_request(:ws_gen_rate_estimate, message: { Token: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", OriginCity: "Birmingham", OriginState: "AL", OriginZip: "35222", OriginCountryCode: "USA", DestinationCity: "New Orleans", DestinationState: "LA", DestinationZip: "70122", DestinCountryCode: "USA", CustomerNumber: "000971733", WhoAmI: "S", PrePaidCollect: "" })
# This will actually send the xml to the server api
response = client.call(:ws_gen_rate_estimate, message: { Token: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", OriginCity: "Birmingham", OriginState: "AL", OriginZip: "35222", OriginCountryCode: "USA", DestinationCity: "New Orleans", DestinationState: "LA", DestinationZip: "70122", DestinCountryCode: "USA", CustomerNumber: "000971733", WhoAmI: "S", PrePaidCollect: "" })
render json: {
"this is a": "test",
"client_request": request.body,
"client_response": response.body,
}, status: :ok
對此請求的響應帶有“ error_message”,表示我的令牌無效,但是我知道我有一個有效的令牌,並且知道該令牌已完全粘貼在該代碼中。 這是XML發送到服務器的樣子:
<?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=\"http://tempuri.org/wsGenRateEstimate/\" xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">
<env:Body>
<tns:RateEstimateRequestVO>
<token>XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX</token>
<originCity>Birmingham</originCity>
<originState>AL</originState>
<originZip>35222</originZip>
<originCountryCode>USA</originCountryCode>
<destinationCity>New Orleans</destinationCity>
<destinationState>LA</destinationState>
<destinationZip>70122</destinationZip>
<destinCountryCode>USA</destinCountryCode>
<customerNumber>000971733</customerNumber>
<whoAmI>S</whoAmI>
<prePaidCollect></prePaidCollect>
</tns:RateEstimateRequestVO>
</env:Body>
</env:Envelope>
標簽名稱和名稱空間與示例所要求的不同。 這會導致API找不到令牌嗎? 如果是這樣,savon gem是否提供更改標簽名稱或屬性的選項?
您的Savon正在生產的節點稱為token
而服務期望接收Token
(大寫T
)。 如您所見,所有節點都存在相同的問題:它們以首字母小寫發送,並且期望以大寫形式接收。
Savon 默認會將鍵轉換為lowerCamelcase。 您可以在全局convert_request_keys_to
更改此行為。
全局選項包括:camelcase
, :lower_camelcase
, :upcase
和:none
。 在您的問題中,您應該使用:camelcase
。
require 'savon'
client = Savon.client(
wsdl: "http://wsportal.aaacooper.com:8188/wsGenRateEstimate.wsdl",
convert_request_keys_to: :camelcase
)
這樣,您可以將請求發送為:
request = client.build_request(:ws_gen_rate_estimate, message: { token: "XXXXX", origin_city: "Birmingham" })
並將密鑰轉換為相應的駝峰式密碼。 請注意,您可以將它們snakecase
,並將正確轉換。
這可能會有所幫助,因為它在railscasts
對我railscasts
Gyoku.convert_symbols_to :camelcase
例如:
def initialize(test)
Gyoku.convert_symbols_to :camelcase
client = Savon::Client.new("wsdl_link")
response = client.request :web, :get_actions, body: { "test" => test }
if response.success?
// enter code
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.