【问题标题】:How to pass Array as parameter to SOAP in Ruby如何在Ruby中将数组作为参数传递给SOAP
【发布时间】:2011-03-17 07:32:51
【问题描述】:

目前我正在使用 Savon 在 ruby​​ 中使用 WebService。 它工作得很好,但我很难传递参数 SOAP 数组类型的参数。以下代码无法正常工作:

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

如果您能解决我的问题或提出替代方案,我将不胜感激 ruby&soap 库

【问题讨论】:

    标签: ruby soap savon


    【解决方案1】:

    我遇到了类似的问题。我必须将字符串数组作为请求的两个参数发送。我使用的是 Savon 版本 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 并检查正确的请求应该是什么样子。

    【讨论】:

      【解决方案2】:

      我只是偶然发现了同样的问题,对我有用的临时解决方法如下:

      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
      

      【讨论】:

      • 很好的建议,尽管它可能并不总是足够的。一些 SOAP 服务器还要求提供 SOAP-ENC:Array 或类似属性。 Savon 对数组的支持仍然非常有限。
      • 谢谢!节省了很多时间!
      猜你喜欢
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 2021-06-02
      • 2020-01-06
      • 2014-01-12
      • 2011-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多