my recent reads..

Atomic Accidents: A History of Nuclear Meltdowns and Disasters; From the Ozark Mountains to Fukushima
Power Sources and Supplies: World Class Designs
Red Storm Rising
Locked On
Analog Circuits Cookbook
The Teeth Of The Tiger
Sharpe's Gold
Without Remorse
Practical Oscillator Handbook
Red Rabbit

Thursday, October 18, 2007

Registering namespaces with SOAP::Lite

About time to post about something ... too many OpEd pieces of late!

An interesting question came up on the soaplite mailing list concerning how to modify the namespaces registered in the SOAP envelope. Documentation is not especially clear on this point.

Of course, a quick hack is to attach a full namespace to an element directly, as in:
SOAP::Data->name('itemName')->attr({'xmlns:mns' => 'http://my.namespace'})

With version SOAP::Lite 0.65 and above, the register_ns serializer method helps to correctly construct the envelope, as shown in the following example:
#!/usr/bin/perl -w
# $Id$
#

use strict;

#SOAP::serializer->register_ns requires 0.65
use SOAP::Lite 0.65 +trace => 'debug';

my $soap = SOAP::Lite
->proxy( 'http://localhost/blah/DummyService' );

my $serializer = $soap->serializer();
$serializer->register_ns( 'http://my.namespace', 'mns' );

my $som = $soap->call(
SOAP::Data
->name('mns:test')
=> SOAP::Data->name('mns:description' => 'an item in my namespace')->type('mns:mytype')
);

This generates the following SOAP request:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:mns="http://my.namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<mns:test>
<mns:description xsi:type="mns:mytype">an item in my namespace</mns:description>
</mns:test>
</soap:Body>
</soap:Envelope>

5 comments:

Anonymous said...

thanks for your post and your example. Adding name spaces saved me a lot of trouble. The only other option I ran into with a particular SOAP server was to use LWP/HTTP and craft my own POST request to the server.

Thanks!

Anonymous said...

Thanks a million! I couldn't figure out why my outdated versions of SOAP::Lite worked with my service, but the latest version resulted in data validation errors. This example set me straight after a day and a half of missteps.

Unknown said...

hth!

Score +2 for crufty old blog posts on the internet;-)

Anonymous said...

You saved my day. thanks.

Anonymous said...

Awesome. I wouldn't have got my SOAP client working without your post. Thank you.