title: Trying to build the correct SOAP request body with Savon 2
body: <p>I need to generate the following SOAP request with savon to an external API, this is an example i got of what a successful request would be:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;env:Envelope
  xmlns:env="http://www.w3.org/2003/05/soap-envelope"
  xmlns:ns1="urn:ConsultarSucursales"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:ns2="http://xml.apache.org/xml-soap"
  xmlns:ns3="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
  xmlns:enc="http://www.w3.org/2003/05/soap-encoding"&gt;
  &lt;env:Header&gt;
      &lt;ns3:Security env:mustUnderstand="true"&gt;
          &lt;ns3:UsernameToken&gt;
              &lt;ns3:Username&gt;XXXX&lt;/ns3:Username&gt;
              &lt;ns3:Password&gt;XXXX&lt;/ns3:Password&gt;
          &lt;/ns3:UsernameToken&gt;
      &lt;/ns3:Security&gt;
  &lt;/env:Header&gt;
  &lt;env:Body&gt;
      &lt;ns1:ConsultarSucursales env:encodingStyle="http://www.w3.org/2003/05/soap-encoding"&gt;
          &lt;Consulta xsi:type="ns2:Map"&gt;
              &lt;item&gt;
                  &lt;key xsi:type="xsd:string"&gt;consulta&lt;/key&gt;
                  &lt;value xsi:type="ns2:Map"&gt;
                      &lt;item&gt;
                          &lt;key xsi:type="xsd:string"&gt;Localidad&lt;/key&gt;
                          &lt;value xsi:type="xsd:string"&gt;&lt;/value&gt;
                      &lt;/item&gt;
                      &lt;item&gt;
                          &lt;key xsi:type="xsd:string"&gt;CodigoPostal&lt;/key&gt;
                          &lt;value xsi:type="xsd:string"&gt;1406&lt;/value&gt;
                      &lt;/item&gt;
                      &lt;item&gt;
                          &lt;key xsi:type="xsd:string"&gt;Provincia&lt;/key&gt;
                          &lt;value xsi:type="xsd:string"&gt;&lt;/value&gt;
                      &lt;/item&gt;
                  &lt;/value&gt;
              &lt;/item&gt;
          &lt;/Consulta&gt;
      &lt;/ns1:ConsultarSucursales&gt;
  &lt;/env:Body&gt;
  </code></pre>
  
  <p></p>
  
  <p>With Savon i used the following code:</p>
  
  <pre><code>require 'savon'
  
  client = Savon.client do
    wsdl 'https://sucursales.andreani.com/ws?wsdl'
    wsse_auth('XXXXX', 'XXXXX')
    convert_request_keys_to :camelcase
    soap_version 2
    env_namespace :soapenv
    namespace_identifier :ser
    ssl_verify_mode :none
    log true
  end
  
  response = client.call :consultar_sucursales, :message =&gt; {:consulta =&gt; {:codigo_postal =&gt; '1406', 'Localidad': nil, 'Provincia': nil}}
  </code></pre>
  
  <p>That generates the following SOAP raw request format:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;soapenv:Envelope
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:ser="urn:ConsultarSucursales"
  xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"&gt;
  &lt;soapenv:Header&gt;
      &lt;wsse:Security
          xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"&gt;
          &lt;wsse:UsernameToken wsu:Id="UsernameToken-1"
              xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;
              &lt;wsse:Username&gt;XXXXX&lt;/wsse:Username&gt;
              &lt;wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"&gt;XXXXX&lt;/wsse:Password&gt;
          &lt;/wsse:UsernameToken&gt;
      &lt;/wsse:Security&gt;
  &lt;/soapenv:Header&gt;
  &lt;soapenv:Body&gt;
      &lt;ser:ConsultarSucursales&gt;
          &lt;Consulta&gt;
              &lt;CodigoPostal&gt;1406&lt;/CodigoPostal&gt;
              &lt;Localidad xsi:nil="true"/&gt;
              &lt;Provincia xsi:nil="true"/&gt;
          &lt;/Consulta&gt;
      &lt;/ser:ConsultarSucursales&gt;
  &lt;/soapenv:Body&gt;
  </code></pre>
  
  <p>This request is yielding a 500 error for now.</p>
  
  <p>I'm a bit lost on how to generate the correct request. Should set the namespaces manually in the request? I'm missing something else with the request i'm generating with Savon?</p>
  
  <p><strong>Update on the question:</strong></p>
  
  <p>After modifying a bit the request i managed to have the same request header as it's expected in the API server, but still, having 500 issue when i send it to the server.</p>
  
  <p>The request code i use with Savon now is the following</p>
  
  <pre><code>namespaces = {
  "xmlns:ns1" =&gt;"urn:ConsultarSucursales",
  "xmlns:xsi" =&gt;"http://www.w3.org/2001/XMLSchema-instance",
  "xmlns:xsd" =&gt;"http://www.w3.org/2001/XMLSchema",
  "xmlns:ns2" =&gt;"http://xml.apache.org/xml-soap",
  "xmlns:ns3" =&gt;"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
  "xmlns:enc" =&gt;"http://www.w3.org/2003/05/soap-encoding"
  }
  
  h = %{&lt;ns3:Security env:mustUnderstand="true"&gt;&lt;ns3:UsernameToken&gt;&lt;ns3:Username&gt;XXXXX&lt;/ns3:Username&gt;&lt;ns3:Password&gt;XXXX&lt;/ns3:Password&gt;&lt;/ns3:UsernameToken&gt;&lt;/ns3:Security&gt;}
  
  client = Savon.client do
    wsdl 'https://sucursales.andreani.com/ws?wsdl'
    convert_request_keys_to :camelcase
    soap_version 2
    namespaces namespaces
    env_namespace :env
    log true
    soap_header h
  end
  </code></pre>
  
  <p>generating this SOAP request:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;env:Envelope
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:tns="urn:ConsultarSucursales"
  xmlns:env="http://www.w3.org/2003/05/soap-envelope"
  xmlns:ns1="urn:ConsultarSucursales"
  xmlns:ns2="http://xml.apache.org/xml-soap"
  xmlns:ns3="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
  xmlns:enc="http://www.w3.org/2003/05/soap-encoding"&gt;
  &lt;env:Header&gt;
      &lt;ns3:Security env:mustUnderstand="true"&gt;
          &lt;ns3:UsernameToken&gt;
              &lt;ns3:Username&gt;XXXXXX&lt;/ns3:Username&gt;
              &lt;ns3:Password&gt;XXXXXX&lt;/ns3:Password&gt;
          &lt;/ns3:UsernameToken&gt;
      &lt;/ns3:Security&gt;
  &lt;/env:Header&gt;
  &lt;env:Body&gt;
      &lt;tns:ConsultarSucursales&gt;
          &lt;Consulta&gt;
              &lt;CodigoPostal&gt;1406&lt;/CodigoPostal&gt;
              &lt;Localidad xsi:nil="true"/&gt;
              &lt;Provincia xsi:nil="true"/&gt;
          &lt;/Consulta&gt;
      &lt;/tns:ConsultarSucursales&gt;
  &lt;/env:Body&gt;
  &lt;/env:Envelope&gt;
  </code></pre>