17

I'm trying to write code in Ruby with the Savon gem (v2) that fetches account information from a SOAP api, but I'm having an issue with passing an Array.

CampaignIds is supposed to be an array of integers.

Here is my code:

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)

which produces the following request:

<?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>

whereas, if I play around in SOUP UI, the request should look like:

<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>

Any Ideas?

0

2 Answers 2

29

You can try this syntax :

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

That'll produce this output :

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

Hope this helps.

2
  • 2
    Thanks, I was able to figure it out by doing "CampaignIds" => { "ins0:int" = > 3 }
    – Sam Lehman
    Jan 8, 2014 at 20:59
  • I tired ins0: int, I tried int, with both I get: soapFault: No open start element, when trying to write end element. I then tried in soap UI just adding type="int" and xsi:type="xsd:int" as an attribute in the element I need to be an int. With the attributes I got: 422 Unprocessable Entity: The server encountered an error. All of these I tried with literal and encoded. Please advise Apr 16, 2014 at 1:55
1

I just experienced the same issue and found a solution, that's not dependent on having a permissive SOAP endpoint. You can configure this behavior as a global using the unwrap key. This key is given as an option to Gyoku which generates the XML for Savon.

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

Although the issue is old, better late than never.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.