The SOAP Protocol

SOAP is an XML-based messaging protocol. A SOAP request is an XML document with the following main constituents:

 

Let's look at a skeleton SOAP request:

<?xml version="1.0"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header>
  ... various commands . . .
</SOAP-ENV:Header>
<SOAP-ENV:Body>
... various commands . . .
  <SOAP-ENV:Fault>
... various commands . . .
  </SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

 

Note that the SOAP request is an XML file, which has as its root the Envelope element.

The first line of the Envelope is

<SOAP-ENV:Envelope xmlns:SOAP-EN = "http://schemas.xmlsoap.org/soap/envelope/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

 

This line declares the xmlns:soap namespace, which must always have the value xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/".

Tip

 

A namespace is an identifier used to uniquely group a set of XML elements or attributes, providing a means to qualify their names, so that names in other schemas do not conflict with them.

 

The encodingStyle attribute contains information defining the data types used in the message.

Next appears the Header element, which is optional but must, if present, be the first element in the message. Attributes defined in the Header element define how the message is to be processed by the receiving application.

The body element of the SOAP message contains the message intended for the final recipient.

The serialized method arguments are contained within the SOAP request's body element. The call's XML element must immediately follow the opening XML tag of the SOAP body and must have the same name as the remote method being called.

The body may also contain a Fault element (but no more than one). This element is defined in the SOAP specification and is intended to carry information about any errors that may have occurred. If it exists, it must be a child element of the body element. The Fault element has various child elements including faultcode, faultstring, and detail, which contain specific details of the fault condition.

Code Example of a SOAP Request

Let's see how a typical SOAP request might look:

[View full width]

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAPENV
:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
   <m:GetInvoiceTotal xmlns:m="http://www.somedomain.com/invoices">
      <m:Invoice>77293</m:Invoice>
   </m:GetInvoiceTotal>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

 

In the preceding example, the m:GetInvoiceTotal and m:Invoice elements are specific to the particular application, and are not part of SOAP itself. These elements constitute the message contained in the SOAP envelope.

Let's see what the SOAP response from the web service might look like:

[View full width]

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAPENV
:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>

   <m:ShowInvoiceTotal xmlns:m="http://www.somedomain.com/invoices">
      <m:InvoiceTotal>3295.00</m:InvoiceTotal>
   </m:ShowInvoiceTotal>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

 

Sending the SOAP Request Via HTTP

A SOAP message may be transmitted via HTTP GET or HTTP POST. If sent via HTTP POST, the SOAP message requires at least one HTTP header to be set; this defines the Content-Type:

Content-Type: text/xml

 

After a successful SOAP exchange, you would expect to receive the SOAP response preceded by an appropriate HTTP header:

HTTP/1.1 200 OK
Content-Type: text/xml
Content-Length: yyy
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-
ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
   <m:ShowInvoiceTotal xmlns:m="http://www.somedomain.com/invoices">
      <m:InvoiceTotal>3295.00</m:InvoiceTotal>
   </m:ShowInvoiceTotal>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>