SOAP (Simple Object Access Protocol)
- Used to accomplish internet communication between systems (software, programs, web apps, services etc.)
- HTTP is supported by all internet browsers and servers.
- SOAP therefore uses HTTP to enable this communication.
- SOAP relies on XML for its messaging format.
- Simple!
Common Misconceptions
- There is no such thing as a SOAP Protocol... It's more of a schema (a subset of XML). Therefore, SOAP based web services will typically use HTTP (Port 80) or HTTPS (Port 443).
Architecture
- A SOAP request could be a HTTP GET or a HTTP POST. For example, a HTTP POST request specifies at least two HTTP headers: Content-Type and Content-Length.
- When a GET/POST request is made to a SOAP based web service, the HTTP message will detail two parts: Header information (content length/type, host etc...) and a SOAP Envelope (All in XML)...
Here is a sample HTTP POST request using SOAP...
Code Snippet
- POST /InStock HTTP/1.1
- Host: www.example.org
- Content-Type: application/soap+xml; charset=utf-8
- Content-Length: nnn
- <?xml version="1.0"?>
- <soap:Envelope
- xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
- soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
- <soap:Body xmlns:m="http://www.example.org/stock">
- <m:GetStockPrice>
- <m:StockName>IBM</m:StockName>
- </m:GetStockPrice>
- </soap:Body>
- </soap:Envelope>
End of Code Snippet
Once the service has processed the request, it will provide a response back in the following format...
Code Snippet
- HTTP/1.1 200 OK
- Content-Type: application/soap+xml; charset=utf-8
- Content-Length: nnn
- <?xml version="1.0"?>
- <soap:Envelope
- xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
- soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
- <soap:Body xmlns:m="http://www.example.org/stock">
- <m:GetStockPriceResponse>
- <m:Price>34.5</m:Price>
- </m:GetStockPriceResponse>
- </soap:Body>
- </soap:Envelope>
End of Code Snippet
SOAP Envelope Architecture
A SOAP envelope details header information, message body and any fault codes or statues that may occur/exist. Full schema can be found here: http://www.w3.org/2001/12/soap-envelope
Header (Optional)
Typically, the header is used to hold security information, I.e. authentication into the target system.
Body (Required)
The message body is used to hold all information passed or received from the SOAP service. This includes: Methods, parameters, return values etc...
Example
The Request may look like this...
Code Snippet
- <soap:Body>
- <m:GetPrice xmlns:m="http://www.w3schools.com/prices">
- <m:Item>Apples</m:Item>
- </m:GetPrice>
- </soap:Body>
End of Code Snippet
Once the service has finished processing, probably by checking a database for the price of apples, the SOAP service will return the following message body...
Code Snippet
- <soap:Body>
- <m:GetPriceResponse xmlns:m="http://www.w3schools.com/prices">
- <m:Price>1.90</m:Price>
- </m:GetPriceResponse>
- </soap:Body>
End of Code Snippet
Fault Codes (Optional - Contained in the SOAP Body)
Fault codes are used to indicate error messages. They are entirely optional. If a fault code is present, it will contain the following information: faultcode, faultstring, faultactor, detail.
The following XML displays an example of a SOAP fault in a SOAP response message.
Code Snippet
- <soap:Body>
- <soap:Fault>
- <faultcode xsi:type="xsd:string">soap:Client</faultcode>
- <faultstring xsi:type="xsd:string">
- Failed to locate method (ValidateCreditCard) in class
- (examplesCreditCard) at /usr/local/ActivePerl-5.6/lib/
- site_perl/5.6.0/SOAP/Lite.pm line 1555.
- </faultstring>
- </soap:Fault>
- </soap:Body>
End of Code Snippet
Interoperability
So if your wondering how the heck you can utilise this technology, you already know it's used to solve communication between programs over HTTP, so here's an example...
1. You wish to create a website that displays the latest weather information on the homepage.
2. Rather than spending thousands of pounds/dollars/euro on weather equipment (and learning how to use it!), you decide it would be better to import data from an external website.
3. Rather than "Screen Scraping" (stripping weather information out of the HTML), if we could tap right into their data store, we can display the information however we would like.
4. We now need to find a weather site that exposes a SOAP service for us to consume'... Visit: http://www.service-repository.com/ this is a good public listing of popular SOAP web services.
5. You may see GlobalWeather on the homepage. They provide a nice and easy to use SOAP service to access weather information based on city and country: http://www.webservicex.com/globalweather.asmx... It exposes two methods: GetCitiesByCountry and GetWeather.
6. By clicking on one of these methods, you can view the sample SOAP request and response messages. This provides you with how your HTTP Request should be formatted for the SOAP service to understand your request. It also tells you (from the response) what to expect back from the service. Simple!
NOTE: By understanding the inner workings of SOAP, you can call SOAP services from programming languages/environments that may not DIRECTLY support web service proxies (I.e. iPhone/iOS, Python etc...). So by sending a SOAP request using a standard HTTP Request (which most languages do support), you can achieve the same result.
No comments:
Post a Comment