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

发布于 2024-09-09 19:32:08 字数 311 浏览 6 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星軌x 2024-09-16 19:32:08

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

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

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
掩饰不了的爱 2024-09-16 19:32:08

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文