Вопрос

I'm trying to build a SOAP request using the Savon (version 2).

This is how the request XML should look:

<?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>

When I do this:

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 builds the following 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>

Which is nearly the desired result. The only difference is the message elements should be prefixed with 's', e.g.: <username> should be <sUsername>. This 's' presumably means 'string' as I find 'int' (i.e. integer) and 'l' (i.e. long) in other operations.

Is there a hidden option in Savon to prepend message elements with an abbreviaton of their data types? And if not: how can I make sure the elements are prefixed with their data type abbreviations?

Changing the keys in the value for message doesn't work: s_username: 'my-username' of 'sUsername' => 'my-username'.

Это было полезно?

Решение

Using the documentation and the source I came up with the following solution:

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

And then in the terminal:

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/'}
)

Другие советы

you should use strings for your message hash. You might also want to change the function call.

client.call('cred_LogIn',
        message:
          { 'sUsername': 'my_username',
            'sPassword': 'my_password',
            'sConsumerIdent': 'DZ-Manager_Web-Service_Consumer'},
        attributes: {
           xmlns: 'http://webservice.dz-manager.com/'}
)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top