1

I need to send a request like:

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://tempuri.org/" xmlns:ns2="http://www.w3.org/2005/08/addressing">
   <env:Header>
      <ns2:Action>http://tempuri.org/IDataImportService/Login</ns2:Action>
      <ns2:To>https://URLHERE.svc</ns2:To>
   </env:Header>
   <env:Body>
      <ns1:Login>
         <ns1:userName>USERNAME</ns1:userName>
         <ns1:password>PASSWORD</ns1:password>
      </ns1:Login>
   </env:Body>
</env:Envelope>

To accomplish that I can hack around Savon.

Now my understand is Savon is looking at the WSDL to figure out what namespace to add. There is a thing called element_form_default which slightly does what I want in combination with namespace_identifier and manual patch of soap_header in call E.g.

client = Savon.client(
  wsdl: 'https://urlhere.svc?wsdl',
  log: true,
  soap_version: 2,
  pretty_print_xml: true,
  log_level: :debug,
  namespaces: {
    'xmlns:ns1' => 'http://tempuri.org/',
    'xmlns:ns2' => 'http://www.w3.org/2005/08/addressing'
  },
  namespace_identifier: 'ns1',
  element_form_default: :qualified
)

client.call(
  :login,
  message: {
    'userName' => USERNAME,
    'password' => PASSWORD,
  },
  soap_header: {
    'ns2:Action' => 'http://tempuri.org/IDataImportService/Login',
    'ns2:To' => 'https://urlhere.svc'
  }
)

I'll leave it like this if I didn't have to make another request with 3 different namespaces and pass the context of login...

The questions I have are:

  1. Is there a way to avoid manual patching of those namespaces?
  2. Why is it working correctly in php with SOAP services?

E.g. of what I need co accomplish in next request:

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://schemas.datacontract.org/2004/07/Common.DataContracts" xmlns:ns2="http://tempuri.org/" xmlns:ns3="http://www.w3.org/2005/08/addressing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <env:Header>
      <ns3:Action>http://tempuri.org/IDataImportService/CreateLead</ns3:Action>
      <ns3:To>https://URLHERE.svc</ns3:To>
   </env:Header>
   <env:Body>
      <ns2:CreateLead>
         <ns2:autoItContext>
            <ns1:AddressNamePrimary xsi:nil="true" />
            <ns1:Addresshousenumber xsi:nil="true" />
            <ns1:Addressstreet xsi:nil="true" />
         </ns2:autoItContext>
         <ns2:companyId>111</ns2:companyId>
         <ns2:description></ns2:description>
         <ns2:leadData>
            <ns1:C_Address xsi:nil="true" />
            <ns1:C_Address2 xsi:nil="true" />
            <ns1:C_CellPhoneNumber xsi:nil="true" />
            <ns1:C_City xsi:nil="true" />
            <ns1:C_CprNumber1 xsi:nil="true" />
            <ns1:C_CvrNumber xsi:nil="true" />
            <ns1:C_Email>email@email.com</ns1:C_Email>
         </ns2:leadData>
         <ns2:checkForDuplicates>false</ns2:checkForDuplicates>
         <ns2:listId xsi:nil="true" />
      </ns2:CreateLead>
   </env:Body>
</env:Envelope>
radubogdan
  • 2,744
  • 1
  • 19
  • 27

1 Answers1

0

To answer my own question, I don't think savon can followup namespaces. However after digging in savon's code I realized I can hack around the problem since SOAP requests are not that vital for my application.

Here is a nice solution to deal with multiple namespaces:

  def login_request
    request = Savon::SOAPRequest.new(globals).build(
      soap_action: :login,
      cookies: locals[:cookies],
      headers: locals[:headers]
    )
    request.headers['Content-Type'] = 'application/soap+xml'
    request.url = @url
    request.body = login_xml_string
    HTTPI.post(request)
  end


  def globals
    @_globals ||= Savon::GlobalOptions.new
  end

  def locals
    @_locals ||= Savon::LocalOptions.new
  end

Where login_xml_string is a method that returns a stringify SOAP envelope based on a xml.erb file. E.g.

def login_xml_string
  @view_paths.render(file: 'app/views/path/to/file/login.xml.erb',
    locals: { user_name: @username, password: @password, url: @url}
  ).gsub(/\n[\s+]{0,}/, '')
end

And @view_paths is set to ActionView::Base.new(ActionController::Base.view_paths, {})

The same goes for the rest SOAP actions I need to call.

I'm not saying this is the best approach but it made my life easier dealing with namespaces by passing them in locals.

radubogdan
  • 2,744
  • 1
  • 19
  • 27