传递Savon 2的数组元素(SOAP)

时间:2014-01-07 21:46:26

标签: ruby api soap soapui savon

我正在尝试使用从SOAP api获取帐户信息的Savon gem(v2)在Ruby中编写代码,但是我遇到了传递数组的问题。

CampaignIds应该是一个整数数组。

这是我的代码:

client = Savon.client(wsdl: "https://api7secure.publicaster.com/Pub7APIV1/Campaign.svc?singleWsdl")

message = {
  "EncryptedAccountID" => api_key,
  "APIPassword" => api_password,
  "CampaignIds" => [3,4],
  "StartDate" => yesterday,
  "EndDate" => yesterday,
  "IncludeTests" => false
}

client.call(:get_comparative_report_details_data, message: message)

产生以下请求:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ins0="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:tns="http://BlueSkyFactory.Publicaster7.Public.API" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <env:Body>
      <tns:GetComparativeReportDetailsData>
         <tns:EncryptedAccountID>*****</tns:EncryptedAccountID>
         <tns:APIPassword>*****</tns:APIPassword>
         <tns:CampaignIds>3</tns:CampaignIds>
         <tns:CampaignIds>4</tns:CampaignIds>
         <tns:StartDate>2014-01-06</tns:StartDate>
         <tns:EndDate>2014-01-06</tns:EndDate>
         <tns:IncludeTests>false</tns:IncludeTests>
      </tns:GetComparativeReportDetailsData>
   </env:Body>
</env:Envelope>

然而,如果我在SOUP UI中玩游戏,请求应如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:blu="http://BlueSkyFactory.Publicaster7.Public.API" xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
   <soapenv:Header/>
   <soapenv:Body>
      <blu:GetComparativeReportData>
         <blu:EncryptedAccountID>*****</blu:EncryptedAccountID>
         <blu:APIPassword>*****</blu:APIPassword>
         <blu:CampaignIds>
            <arr:int>3</arr:int>
            <arr:int>4</arr:int>
         </blu:CampaignIds>
         <blu:StartDate>2014-01-06T16:21:47-05:00</blu:StartDate>
         <blu:EndDate>2014-01-07T16:21:47-05:00</blu:EndDate>
         <blu:IncludeTests>false</blu:IncludeTests>
      </blu:GetComparativeReportData>
   </soapenv:Body>
</soapenv:Envelope>

任何想法?

2 个答案:

答案 0 :(得分:22)

您可以尝试以下语法:

message = {
  ...
  "CampaignIds" => {"int" => [3,4]},
  ...
}

那将产生这个输出:

<CampaignIds>
  <int>3</int>
  <int>4</int>
</CampaignIds>

希望这有帮助。

答案 1 :(得分:0)

我刚刚遇到了同样的问题,并找到了解决方案,而这并不依赖于允许的SOAP端点。您可以使用展开键将此行为配置为全局行为。该密钥是Gyoku的一个选项,该选项为Savon生成XML。

client = Savon.client(wsdl: 'https://example.com/wsdl', unwrap: true)
client.call(:cook_meal, message: { 'Ingredients' => ['tomato', 'basil', 'mozzarella'] })

尽管问题已久,但迟到总比没有好。