6.8 SOAP Web Services

Working with SOAP is quite a bit different from working with XML/HTTP because it involves sending and receiving structured messages. Instead of creating requests yourself as a URL, requests are made via XML messages. Here's a typical SOAP request to retrieve a book's details with its ASIN:

<?xml version="1.0" encoding="UTF-8" ?> 
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" [RETURN]
  <namesp1:AsinSearchRequest xmlns:namesp1="urn:PI/DevCentral/SoapService">
    <AsinSearchRequest xsi:type="m:AsinRequest">
      <tag>insert associate tag</tag> 
      <dev-tag>insert developer token</dev-tag> 

You'll notice that although SOAP uses the same bits of information you use when requesting data via the URL-based XML/HTTP method, it wraps the request in substantial structure. Generating these XML requests "by hand" each time you need one would be time consuming; instead, there are packages (typically called SOAP toolkits) in every programming language that do the heavy lifting for you. These toolkits provide a simple interface for setting and calling Web Services functions. Modules like Perl's SOAP::Lite (see [Hack #80] and [Hack #89]) automatically format all of the SOAP requests for you.

Here's an example of a SOAP KeywordSearchRequest from VB.NET, where the query is built by setting each variable name:

Dim AsinReq as new AsinRequest(  )
AsinReq.asin = "0596004478"
AsinReq.tag = "insert associate tag"
AsinReq.type = "lite"
AsinReq.devtag = "insert developer token"

This little bit of code creates an entire SOAP request behind the scenes. In this way, you're working only with the variable information you need to provide rather than the entire SOAP protocol.