How to pass Array as parameter to SOAP in Ruby

纵饮孤独 提交于 2019-12-04 14:30:30

问题


Currently I'm using Savon to work with WebService in ruby. It works pretty well but I have difficulty to pass parameter for argument of SOAP array type. Following code doesn't work properly:

ids = [0,1,2]
client.do_get_items { |soap| soap.body = {
    'item-list' => ids
}

I would appreciate if you can solve my problem or propose an alternative library for ruby&soap


回答1:


I just stumbled on the same problem and the temporary workaround that worked for me is as follows:

ids = [0,1,2]
client.do_get_items { |soap| soap.body = {
  'item-list' => {
    'item1' => 0,
    'item2' => 1,
    'item3' => 2
  }  
}

The names "item1", "item2" shouldn't matter at all.

I use the following helper method to convert regular arrays into SOAP mess:

def soap_array(array)
  returning({}) do |hash|
    array.each_with_index do |e, i|
      hash["item-#{i}"] = e
    end
  end
end



回答2:


I had a similar problem. I had to send array of strings as two of the request's arguments. I used Savon version 2. My final solution looks like this:

class JvMatching

    CLIENT_ID = 'bb_matchnig'

    extend Savon::Model

    operations :query_index

    # arg1, arg 2 - name of parameters that should be arrays of string
    def self.query_index(contents=[], constraints=[], focus='job', result_size=20)
        super(message: { arg0: CLIENT_ID, arg1: { item: contents }, arg2: { item: constraints }, arg3: focus, arg4: result_size })      
    end  

end

What helped me to find the right solution was downloading SOAP UI and checking how proper requests should look like.



来源:https://stackoverflow.com/questions/3271130/how-to-pass-array-as-parameter-to-soap-in-ruby

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!