将缩写数据类型添加到SOAP请求正文元素

时间:2014-03-07 18:53:33

标签: ruby xml soap savon

我正在尝试使用Savon(版本2)构建SOAP请求。

请求XML的外观如下:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <cred_LogIn xmlns="http://webservice.dz-manager.com/">
      <sUsername>string</sUsername>
      <sPassword>string</sPassword>
      <sConsumerIdent>string</sConsumerIdent>
    </cred_LogIn>
  </soap12:Body>
</soap12:Envelope>

当我这样做时:

client = Savon.client(
    wsdl: 'https://online-tools.dz-manager.com/Services/DzM_WebService.asmx?WSDL',
    soap_version: 2,
    pretty_print_xml: true,
    env_namespace: 'soap12',
    namespace_identifier: nil,
    log_level: :debug)

client.call(:cred_log_in,
            message:
              {username: 'my_username',
               password: 'my_password',
               consumer_ident: 'DZ-Manager_Web-Service_Consumer'},
            attributes: {
               xmlns: 'http://webservice.dz-manager.com/'}
)

Savon构建以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<soap12:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://webservice.dz-manager.com/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <cred_LogIn xmlns="http://webservice.dz-manager.com/">
      <username>my_username</username>
      <password>my_password</password>
      <consumerIdent>DZ-Manager_Web-Service_Consumer</consumerIdent>
    </cred_LogIn>
  </soap12:Body>
</soap12:Envelope>

这几乎是理想的结果。唯一的区别是消息元素应该以's'为前缀,例如:<username>应该是<sUsername>。这个''可能意味着'字符串',因为我在其他操作中找到'int'(即整数)和'l'(即long)。

Savon中是否有一个隐藏的选项来预先添加其数据类型缩写的消息元素?如果没有:我怎样才能确保元素的前缀是数据类型缩写?

更改message值的键不起作用:s_username: 'my-username' 'sUsername' => 'my-username'

2 个答案:

答案 0 :(得分:1)

使用documentationsource我提出了以下解决方案:

class ServiceRequest

  def to_s
    builder = Builder::XmlMarkup.new

    builder.tag! :sUsername, "my_username"
    builder.tag! :sPassword, "my_password"
    builder.tag! :sConsumerIdent, "DZ-Manager_Web-Service_Consumer"

    builder
  end

end

然后在终端:

client = Savon.client(
    wsdl: 'https://online-tools.dz-manager.com/Services/DzM_WebService.asmx?WSDL',
    soap_version: 2,
    pretty_print_xml: true,
    env_namespace: 'soap12',
    namespace_identifier: nil,
    log_level: :debug)

client.call(:cred_log_in,
            message: ServiceRequest.new,
            attributes: {
               xmlns: 'http://webservice.dz-manager.com/'}
)

答案 1 :(得分:0)

你应该为你的消息哈希使用字符串。您可能还想更改函数调用。

client.call('cred_LogIn',
        message:
          { 'sUsername': 'my_username',
            'sPassword': 'my_password',
            'sConsumerIdent': 'DZ-Manager_Web-Service_Consumer'},
        attributes: {
           xmlns: 'http://webservice.dz-manager.com/'}
)
相关问题