Saturday, November 19, 2011

SOAP vs REST

Nerd is learning about web services.

What is the difference between SOAP and REST web services?  Nerd finds many different thoughts on the interwebs.  Here are some of them:

From StackOverflow:
 http://stackoverflow.com/questions/1443160/difference-between-rest-and-webservices

SOAP is a protocol for sending/receiving data over HTTP as XML.
A typical WebService will be a few methods an WSDL that describes how to call it. There's no real convention for how these should be structured, so you always need lots of API documentation.
Typically this will be something like (for .net):
  • Http POST to mysite.com/products.asmx/ListAllProducts - returns XML list of products
  • Http POST to mysite.com/products.asmx/GetProduct - returns XML for product based on SOAP XML in the posted content
  • Http POST to mysite.com/products.asmx/UpdateProduct - changes product based on SOAP XML in the posted content
REST is more of a convention for structuring all of your methods:
  • Http GET from mysite.com/products - returns XML or JSON listing all products
  • Http GET from mysite.com/products/14 - returns XML or JSON for product 14
  • Http POST to mysite.com/products/14 - changes product 14 to what you post in the HTML form.
So REST works more like you'd expect browser URLs to. In that way it's more natural and as a convention is much easier to understand. All REST APIs work in a similar way, so you don't spend as long learning the quirks of each system.
REST goes further, so ideally the following would work:
  • Http DELETE to mysite.com/products/14 - removes product 14
  • Http PUT to mysite.com/products - adds a new product
Unfortunately the majority of browsers don't implement these HTTP verbs, so you have to rely on GET and POST for now.

No comments:

Post a Comment