rest vs soap

REST vs SOAP

We recently wrote about the benefits of APIs and web services when building online applications. When it comes to building APIs and web services there are two main contenders: REST (Representational State Transfer), and SOAP (Simple Object Access Protocol).

What is REST?

REST is an architectural style used when building web services. The REST API architecture allows the interaction between two programs, and lets one program request and manipulate resources from another. A REST request for resources uses HTTP verbs, GET, POST, PUT, DELETE, and so on. These requests can be in XML, HTML or JSON among others.

For instance, using the Instagram API example from our previous article, to get a list of comments for a specific Instagram photo an authorised application could send a GET request to the Instagram API at /media/{media-id}/comments.

An application could post a comment on that Instagram photo by sending a POST request to the Instagram API /media/{media-id}/comments.

The application could even delete a specific comment by sending a DELETE request to /media/{media-id}/comments/{comment-id}.

This verb/request architecture is the basis of a RESTful web service, as a resource is being accessed.

What is SOAP?

A SOAP architecture aids in the performance of operations between programs. This communication usually happens using XML requests and HTTP responses. A SOAP message contains the elements ENVELOPE, HEADER, BODY, and FAULT.

The mandatory ENVELOPE element defines the start and end of the XML message request, the HEADER element can contain any optional processing attributes of the message, the mandatory BODY element contains the bulk of the XML request, and the optional FAULT element can be used to handle errors that occur when processing the message.

The body of a typical POST SOAP request might read:

   <SOAP-ENV:Body xmlns:m=”http://www.instagram.com/media/{media-id}/comments” >
<m:PostComment>
<m:CommentName>Your Comment Here</m:CommentName>
</m:PostComment>
</SOAP-ENV:Body>

Benefits of RESTful web services

A REST architecture is more beneficial for web browsers because of its support for JSON, HTML and XML, whereas a SOAP architecture only supports XML. Because of SOAP’s XML-only limitation, a REST architecture has better performance and scalability. REST responses can also be cached, which makes it even quicker the next time a request is sent – SOAP responses cannot be cached. However, caching is only a benefit if the information gathered from the REST request is static, and is not changed or updated often.

A REST architecture is usually a better choice if the application will be interacting with mobile clients, like smartphones.

In contrast to what the ‘S’ in SOAP stands for, a REST architecture is actually simpler than a SOAP architecture, because SOAP often requires writing a lot of code to produce the XML structure, to perform relatively simple tasks. Instead of relying on creating a bespoke XML script for the messaging, REST can make the request directly to the URL, e.g. /media/{media-id}/comments/{comment-id}. It’s arguable that XML is simpler than HTML, but more SOAP/XML code is needed to perform the same request than with REST/HTML.

Benefits of SOAP web services

A SOAP web service is seen as the more secure, heavyweight method, whereas a RESTful web service is seen as the simpler method. SOAP has built-in error handling, SSL support, WS-Security, and support for ACID transactions.

SOAP is language, operating system and platform independent, and supports several transport protocols. This means that for the request between the two programs, it doesn’t matter which operating systems and programming languages are being used. This speeds up the architecture design because the application developer would not need to worry about the variables between programs when submitting the request.

Difference between SOAP and REST

SOAP and REST can both be used to access resources between programs, but there are occasions where either one is more suitable. The increased security features make SOAP the preferred web service for banking applications where security is paramount, but this comes with more complicated and longer development cycles. REST is a much simpler, more efficient architecture that requires less processing, and tends to perform better.

Whether you choose SOAP web services or a RESTful architecture to build an application, you’ll need a powerful, scalable platform to host it. Find out more about Cloud Servers from Fasthosts.

Be Sociable, Share!