# Add namespace to the SOAP Body and Header

**URL:** <https://www.ruby-forum.com/t/add-namespace-to-the-soap-body-and-header/105884>\
**Category:** Ruby\
**Created:** [June 4, 2007, 11:25am UTC](https://www.ruby-forum.com/t/add-namespace-to-the-soap-body-and-header/105884 "2007-06-04T11:25:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Alphen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@David\_Alphen](https://www.ruby-forum.com/u/David_Alphen)\
**Post date:** [June 4, 2007, 11:25am UTC](https://www.ruby-forum.com/t/add-namespace-to-the-soap-body-and-header/105884/1 "2007-06-04T11:25:07Z")

</div>

Hi,

I’m trying to add a namespace to the SOAP Body and Header.  
I send the request:

\<?xml version="1.0" encoding="utf-8" ?\>

\<env:Envelope xmlns:xsd=“[http://www.w3.org/2001/XMLSchema](http://www.w3.org/2001/XMLSchema)”  
xmlns:env=“[http://schemas.xmlsoap.org/soap/envelope/](http://schemas.xmlsoap.org/soap/envelope/)”  
xmlns:xsi=“[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)”\>  
env:Body  
\<n1:Air\_MultiAvailability xmlns:n1=“[http://my.url.com/SAR\_07](http://my.url.com/SAR_07)”\>

But I would like to send:

\<?xml version="1.0" encoding="utf-8" ?\>

\<env:Envelope xmlns:xsd=“[http://www.w3.org/2001/XMLSchema](http://www.w3.org/2001/XMLSchema)”  
xmlns:env=“[http://schemas.xmlsoap.org/soap/envelope/](http://schemas.xmlsoap.org/soap/envelope/)”  
xmlns:xsi=“[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)”\>  
\<soap:Header xmlns="[http://webservices.amadeus.com/definitions](http://webservices.amadeus.com/definitions)  
"\>\</soap:Header\>  
\<env:Body xmlns=“[http://my.url.com/SAR\_07](http://my.url.com/SAR_07)”\>  
\<Air\_MultiAvailability\>

I have tested the last one and it works.  
So I need several things:

\_add namespaces to the Header and Body tags,  
\_plus the Header tag is empty, it does not contain any element (I have  
seen  
lots of example that add element to the header but not just an empty  
header):

\<soap:Header xmlns="[http://webservices.amadeus.com/definitions](http://webservices.amadeus.com/definitions)  
"\>\</soap:Header\>

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![David\_Alphen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@David\_Alphen](https://www.ruby-forum.com/u/David_Alphen)\
**Post date:** [June 5, 2007, 6:34am UTC](https://www.ruby-forum.com/t/add-namespace-to-the-soap-body-and-header/105884/2 "2007-06-05T06:34:38Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Hi,

David Alphen wrote:

> So I need several things:
> 
> \_add namespaces to the Header and Body tags,  
> \_plus the Header tag is empty, it does not contain any element (I have seen  
> lots of example that add element to the header but not just an empty  
> header):
> 
> \<soap:Header xmlns="[http://webservices.amadeus.com/definitions](http://webservices.amadeus.com/definitions)  
> "\>\</soap:Header\>

Following code samples require soap4r-1.5.6 RC2. You need to get 1.5.6  
RC2 from [http://dev.ctor.org/soap4r/wiki#a2007-06-05:1.5.6-RC2](http://dev.ctor.org/soap4r/wiki#a2007-06-05:1.5.6-RC2)

1. empty SOAP Header

soap4r-1.5.5 cannot send empty SOAPHeader. With 1.5.6, you can do that  
like this;

require ‘soap/rpc/driver’  
require ‘soap/header/handler’

class EmptyHeaderHandler \< SOAP::Header::Handler  
def on\_outbound(header)  
# dump SOAPHeader even if it has no item  
header.force\_encode = true  
header.extraattr[‘xmlns’] = ‘urn:foo’  
# no item needed  
nil  
end  
end

driver = SOAP::RPC::Driver.new(‘[http://localhost:7171/soapendpoint](http://localhost:7171/soapendpoint)’)  
driver.wiredump\_dev = STDOUT  
driver.headerhandler \<\< EmptyHeaderHandler.new(XSD::QName::EMPTY)  
driver.add\_method(‘nop’).call

1. free namespace definition control

At first, XML processors must handle both A and B as the same.

A. env:Body  
\<n1:Air\_MultiAvailability xmlns:n1=“[http://my.url.com/SAR\_07](http://my.url.com/SAR_07)”/\>  
\</env:Body\>  
B. \<env:Body xmlns=“[http://my.url.com/SAR\_07](http://my.url.com/SAR_07)”\>  
\<Air\_MultiAvailability/\>  
\</env:Body\>

But I can understand that there’s a case we cannot change other party’s  
SOAP implementation. For the case of this, you need to construct XML by  
yourself like this;

driver = SOAP::RPC::Driver.new(‘[http://localhost:7171/soapendpoint](http://localhost:7171/soapendpoint)’)  
header = SOAP::SOAPHeader.new  
header.extraattr[‘xmlns’] = ‘urn:foo’  
header.force\_encode = true  
require ‘rexml/document’  
body = SOAP::SOAPBody.new(  
REXML::Document.new(‘\<Air\_MultiAvailability/\>’))  
body.extraattr[‘xmlns’] = ‘urn:foo’  
driver.invoke(header, body)

Of course you’d better to ask the other party to fix the implementation.

Regards,  
// NaHi

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.5 (Cygwin)

iQEVAwUBRmTnUh9L2jg5EEGlAQKpgAf9F4o7yMwFp0OGRp9clEupC/ex5Fphejyv  
uatlBb2nH/4ZoBl4ufZQdhrYNYics2E9TZ+TmdZujCfV/v8kgxAPys1OWYh2enQB  
va6ayndvcD+bTDG7fRdoQQmOW+ZYDePRm814pCcuqZEYwOxTjYEilHTBY+cw/sdU  
w5qIKc8B5lh6OXcbUF3ezWY/XGChrNK6flGKRzuRt8zrDFgIsyf2F75QkH8DXxrR  
4yS1lTQ4AHWrpqBrH2EnWjJmILCCw8ZqBzaXC4XfYOizmBxrsyhx9tULQkClD2tR  
ai5iky6cx9nUuPcu2oaiZryDQ02dt7gx8W1fwfrnwzyk3BFS+6ps+w==  
=ree6  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![David\_Alphen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@David\_Alphen](https://www.ruby-forum.com/u/David_Alphen)\
**Post date:** [June 5, 2007, 1:58pm UTC](https://www.ruby-forum.com/t/add-namespace-to-the-soap-body-and-header/105884/3 "2007-06-05T13:58:52Z")

</div>

Hello,

Thank you,

I’ve updated to 1.5.6 RC2 and it works now, here is my code:

/\* Start code \*/

require ‘soap/header/handler’  
require ‘http-access2’  
require ‘soap/wsdlDriver’

wsdl = “myWsdl.wsdl”

factory = SOAP::WSDLDriverFactory.new(wsdl)  
driver = factory.create\_rpc\_driver

# save the SOAP message

driver.wiredump\_file\_base = File.join(File.dirname ( **FILE** ), “tmp”)  
driver.wiredump\_dev = STDERR if $DEBUG

class EmptyHeaderHandler \< SOAP::Header::Handler

def on\_outbound(header)  
# dump SOAPHeader even if it has no item  
header.force\_encode = true  
header.extraattr[‘xmlns’] = ‘urn:foo’  
# no item needed  
nil  
end

end

driver.headerhandler \<\< EmptyHeaderHandler.new(XSD::QName::EMPTY)

parameters = { #… my parameters }

req = driver.Air\_MultiAvailability(parameters)

/\* End of code \*/

Well, I have an other question I’ll ask in a new topic about wsdl2ruby.

++
