如何将数组作为参数传递给Ruby中的SOAP

问题描述:

当前我正在使用Savon在Ruby中使用WebService。 它工作得很好,但我很难通过参数 参数的SOAP数组类型。下面的代码无法正常工作:如何将数组作为参数传递给Ruby中的SOAP

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

我将不胜感激,如果你能解决我的问题或提出替代 库红宝石&肥皂

我只是无意中发现了同样的问题和暂时性的解决方法为我工作如下:

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

名称“item1”,“item2”应该不重要。

我用下面的辅助方法,常规数组转化为SOAP烂摊子:

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

很好的建议,但它可能并不总是足够的。有些SOAP服务器还要求提供SOAP-ENC:Array或类似的属性。 Savon对阵列的支持仍然非常有限。 – tokland 2010-09-16 17:03:57

我也有类似的问题。我必须发送字符串数组作为请求的两个参数。我用萨翁第2版。我的最终解决方案是这样的:

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 

什么帮我找到合适的解决方案正在下载SOAP UI和检查适当的请求应该是什么样子等。