我正在处理soap api,该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是否提供更改标签名称或属性的选项?
答案 0 :(得分:1)
您的Savon正在生成的节点称为token
,而该服务预期会收到Token
(以大写T
)。如您所见,所有节点都存在相同的问题:它们以首字母小写发送,并且期望以大写接收。
Savon by default会将密钥转换为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
中编写它们,并将其正确转换。
答案 1 :(得分:0)
这可能会有所帮助,因为它适用于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