在 ruby 中使用 Savon 时的 Soap 请求格式
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 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
里面,会正确转换的
这可能会有所帮助,因为它对我有用 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
我正在处理一个 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 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
里面,会正确转换的
这可能会有所帮助,因为它对我有用 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