Savon Rails-如何传递多个参数?

时间:2017-11-01 00:33:08

标签: savon

我有这段代码:

client = Savon.client(wsdl: wsdl)

response = client.call(:post, message: {
  login: {
    userName: username,
    password: password
  },
  data: {
    parameters: {
      key: "firstName",
      value: "Joe"
    }
})

这有效,但我需要发送多个参数。如果我有多个parameters: {mydata}字段,则它不起作用,因为哈希必须具有唯一键...

如何让Savon发送多个参数字段?

1 个答案:

答案 0 :(得分:0)

我仍然不知道正确的方法,但message参数可以是XML字符串,所以我最终得到了以下解决方案:

client = Savon.client(wsdl: wsdl)
response = client.call(:post, message: build_xml)

def build_xml
  "
  #{login_xml}
  <data>
    <code>CUSTOMER_DETAILS</code>
    " + test_data.map { |h| parameter_xml(h.first, h.last) }.join("") + "
  </data>
  "
end

def login_xml
  "
  <login>
    <userName>#{@xml_username}</userName>
    <password>#{@xml_password}</password>
  </login>
  "
end

def parameter_xml(key, val)
  "
  <parameters>
      <key>#{key}</key>
      <value>#{val}</value>
  </parameters>
  "
end