When you are just getting started learning about Web services, it can seem like a daunting task. Just looking through the list of architectures to choose from can be confusing and intimidating. In this article, I will introduce you to REST and the concept of RESTful web services so that you can begin using them like a pro. Since REST has gained acceptance as a better, easier alternative to SOAP and the Web Services Description Language, you will find that it isn’t as hard to understandas you may have thought.
What is HTTP?
HTTP is a communications protocol that is used on the internet as a means of back-and-forth conversation between the client computer and the server. The protocol has a set of rules that defines how messages can be sent and how to reply. HTTP messages are how this communication is carried out and contain two parts: header and body.
There are special HTTP verbs that are used in the header of a message to tell the server what to do with the data in the URL. The verbs you can use are GET, POST, PUT and DELETE:
GET – Instructs the server to return data from the server to the client. This is the command that browsers use to return web pages when you go to a URL or click on the link to a page.
PUT – Creates or updates a resource identified by a URL. The data that you send with the PUT command can be used by the server in updating or creating the resource.
DELETE – Used when you want to delete a resource.
POST -Used when you want the server to do some kind of processing of the information in the body.
Representational State Transfer (REST) is a set of architectural principles that you can use to design Web services for the transfer of data across HTTP. It is the most widely used system by large Web 2.0 provides such as Yahoo, Google and Facebook. This architecture style allows us to setup resources, or individual services, that can be identified by a URL and use the HTTP operations we talked about above, such as GET, PUT, POST and DELETE. Some of the main features of REST are:
- A clear separation between the client and the server. This helps improve the portability of the application in the client and the scalability of the server.
- RESTful Web services are stateless. If the server needs to restart, the application can be resilient enough to maintain what it needs to keep data moving and resources available.
- Applications can cache data, however it is required that responses to requests be implicitly or explicitly labeled as cacheable or non-cacheable.
Because REST runs on HTTP and all resources are available via URLs, it is easy for companies to expose their Web services so users can access them. REST usually returns either a type of XML or Object Encoding like JSON. For a Web service to be considered “RESTful”, it should promote the use of HTTP methods explicitly and have a one-to-one mapping between create, read, update and delete (CRUD) operations. Here are the guidelines for using the HTTP methods for these operations:
- Creating resources, use POST
- Retrieving a resource, use GET
- Change the state of a resource, or updating, use PUT
- Removing or deleting a resource, use DELETE
Designing a Web service that uses the HTTP methods outside of this set of guidelines is considered bad practice and creates flaws in the design of the API.
A REST Web service should be able to scale to meet high-performance demands and this often means that it can work just as well on one server as twenty. Most REST APIs these days are hosted in the cloud, where one server can handle a request just as well as another, so that requests can be load-balanced between servers and the one with the most availability is the one that handles the request. This allows your application to have almost uninterrupted access to the API at all times and creates a more robust processing environment.
However, REST APIs should also be stateless, which means that they do not require the server to store any kind of state or context information about the application. All of the processing should be done in complete, independent requests so that servers can balance the work between themselves. This improves performance, simplifies the design and removes the need to synchronize between the server and the client because there is no persistent state data.
Libraries and APIs
REST Web services allow you to create your client application without having to do a lot of the heavy lifting that will be taking place in the service. REST APIs can be written or used in almost any language, which allows you to choose which language you are most comfortable with when writing your application. Here are some popular ones:
- Ruby with Rails or Sinatra
Since REST lets you interact with different clients such as mobile phones and other websites, you can create client applications more quickly and with less overhead. Otherwise, you would need to set up that functionality yourself, or in the case of APIs for sites like Twitter, you wouldn’t be able to have the access that is available without them.
While HTTP was created to be used as a method of communication between a client and a server, REST just builds on top of that architecture so you are able to write applications that take advantage of that communication system in the best way possible. By using REST you can organize interactions between independent systems with ease, which allows you to focus on the problems of your client application and not those of your outside resources.