Project Links

Recommended Books

Donate


« How to access attributes in a response | Main | Installation Instructions »

Composing messages with raw XML...

I was faced with the dilemna of having an XML document that I had scraped from a web page that I wanted to encode into a SOAP Envelope, but I couldn't figure out how to do it, so I asked the SOAP::Lite mailing list (a long time ago) and got my answer. And since several others (including this one, and this one) have asked the same question. Since the question is so popular - here is how you do it...

Long before Amazon built SOAP interfaces to their product catalog, they had a REST interface. REST vs. SOAP aside, I really wanted a SOAP interface. So I used SOAP::Lite to marshall SOAP requests to and from the REST service. All I was essentially doing was taking a SOAP request, and composing a URL to scrape, scraping it, and then placing the raw XML I scraped into a SOAP response.

FYI: Full source of service available here.

When it came time to tackle the packaging of the raw XML I was at first concerned that I would have to instantiate a parser, or rebuilt the XML document in some way. Luckily, Paul (the creator of SOAP::Lite) set me straight.

To package raw XML in a SOAP::Data element, simply:

my $xml_content = fetch($url);
$xml_content =~ s/\<\?xml.*\?\>\n?//; # strip the <?xml blah?>
$xml_content =~ s/\<\!.*\>\n?//;      # strip 
return SOAP::Data->type('xml' => $xml_content);

In the above case I was dealing with a complete document, so I had to first strip the parts of the xml document I didn't need. Once that was done, just set the type to 'xml' and the value to your document... SOAP::Lite will do the rest.

Comments

Thanks, this helped alot.

I'm looking for a method to send a RAW XML request to a SOAP server with SOAP::Lite.

I have the whole XML request in the variable $rawxml. No modification is needed.

How can I send this $rawxml request to the server?

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Don't Hate the Ads

Blog powered by TypePad