Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with a collection of response elements using Savon
    text
    copied!<p>I am attempting to use Savon to communicate with a legacy web service using SOAP. I am having an issue with regards to how Savon constructs a hash out of the response envelope.</p> <pre><code>client = Savon.client("http://my-service-endpoint/?wsdl") response = client.request :my_soap_operation do soap.body do |xml| xml.in("test") end end response.to_hash[:my_soap_operation_response][:out].each do |a| puts "Return element: #{a}" puts "Name #{a[:name]}" puts "Type #{a[:type]}" puts "Code #{a[:code]}" end </code></pre> <p>end </p> <p>If the SOAP response contains more than 1 'out' element, such as this:</p> <pre><code>&lt;mySoapOperationResponse&gt; &lt;out&gt; &lt;code&gt;C1947944&lt;/code&gt; &lt;name&gt;Use&lt;/name&gt; &lt;type&gt;type 1&lt;/type&gt; &lt;/out&gt; &lt;out&gt; &lt;code&gt;C1947946&lt;/code&gt; &lt;name&gt;name 2&lt;/name&gt; &lt;type&gt;type 2&lt;/type&gt; &lt;/out&gt; &lt;out&gt; &lt;code&gt;C1947947&lt;/code&gt; &lt;name&gt;name 2&lt;/name&gt; &lt;type&gt;type 3&lt;/type&gt; &lt;/out&gt; &lt;/mySoapOperationResponse&gt; </code></pre> <p>The output is as expected:</p> <pre><code>Return element: {:code=&gt;"C1947944", :name=&gt;"Use", :type=&gt;"type 1"} Name Use Type type 1 Code C1947944 Return element: {:code=&gt;"C1947946", :name=&gt;"name 2", :type=&gt;"type 2"} Name name 2 Type type 2 Code C1947946 Return element: {:code=&gt;"C1947947", :name=&gt;"name 2", :type=&gt;"type 3"} Name name 2 Type type 3 Code C1947947 </code></pre> <p>However, if the SOAP response only contains a single 'out' element, such as this:</p> <pre><code>&lt;mySoapOperationResponse&gt; &lt;out&gt; &lt;code&gt;C1947944&lt;/code&gt; &lt;name&gt;name 1&lt;/name&gt; &lt;type&gt;type 1&lt;/type&gt; &lt;/out&gt; &lt;/mySoapOperationResponse&gt; </code></pre> <p>I see this in the console</p> <pre><code>Return element: [:code, "C1947944"] </code></pre> <p>... and then I get the error message</p> <pre><code>can't convert Symbol into Integer </code></pre> <p>It would appear as though the :name and :type elements are not present in the SOAP response, however I can see them via the response XML message.</p> <p>I am using Savon v1.2.0.</p> <p><strong>Addendum</strong></p> <p>I added the following code to try to further debug the issue:</p> <pre><code> response = client.request :my_soap_operation do soap.body do |xml| xml.in("test") end end pp response.to_hash response.to_hash[:my_soap_operation_response][:out].each do |a| pp a puts "Return element: #{a}" puts "Name #{a[:name]}" puts "Type #{a[:type]}" puts "Code #{a[:code]}" </code></pre> <p>And I can see the elements being pretty-printed in the response hash. Here is a snapshot of the console:</p> <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt; &lt;soapenv:Body&gt; &lt;mySoapOperationResponse&gt; &lt;out&gt; &lt;code&gt;C1947944&lt;/code&gt; &lt;name&gt;name 1&lt;/name&gt; &lt;type&gt;type 1&lt;/type&gt; &lt;/out&gt; &lt;/mySoapOperationResponse&gt; &lt;/soapenv:Body&gt; &lt;/soapenv:Envelope&gt; {:my_soap_operation_response=&gt; {:out=&gt;{:code=&gt;"C1947944", :name=&gt;"name 1", :type=&gt;"type 1"}, :@xmlns=&gt;"http://namespace.removed.com/"}} [:code, "C1947944"] Return element: [:code, "C1947944"] </code></pre> <p>However it seems like inside the .each loop (looping over each 'out' element), only the first element can be accessed?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload