Rest simply stands for Representational State Transfer. It is the convention for building http services. Using http principle, we can provide support for client applications to Read, Create, Update and Delete data. We refers to this operations as CRUD operations.
For example, consider an online radio streaming service that can serve various client applications with a list of radio stations. Such client applications include Mobile apps on Android, iOS and Windows. In order to manage the list of radio stations on the client app, we will need to expose a service on the server, which we usually refer to as an endpoint of the format below.
With the above url, the client application can send http requests to the endpoint, in order to communicate with the server. The endpoint address can either be http or https. The use of either http or https solely depends on the kind of application you are building, as well as its requirements. For instance, in situations where sensitive informations are accessed such as credit card information, it is a good practice to enable data exchange over a secure channel like https.
The second part is the domain of the application, which also represents the base url of the web service.
Thirdly, we have the ‘/api/v1‘. This is just a convention and part of standard practices to follow when building Restful APIs. The name api helps in understanding the part to the application endpoints on the server, while the v1, v2 and so on refers to the versions of the web service.
The last part is the ‘/stations’, which refers to the resource. All operations such as creating a new radio station, updating an existing station, reading the list of radio stations and also deleting radio station information from the database will be performed by sending an http request to the station endpoint.
HTTP Request Types
The type of the http request determines the kind of the operation that will be carried out. In other words, every HTTP request has a verb or method for determining its types. Some of the popular standard http methods includes:
- GET – for retrieving list of data
- POST – for creating a new data
- PUT – for updating an existing data
- DELETE – for deleting a data
Using the online radio streaming application discussed earlier, let’s explain how each of the http request above can come into play, together with the possible response from the server.
HTTP GET REQUEST – GET LIST OF STATIONS
GET REQUEST – GET A STATION
PUT REQUEST – UPDATE A STATION
Notice that the object property that we want to update is included in the request body, together with the object ID. This is important in order to successfully update the object property, in this case, the name of the radio station.
DELETE REQUEST – DELETE A STATION
For the DELETE request, there is no need to include the station object in the request body. This is because, all we need to delete an object is the ID of the object.
POST REQUEST – CREATE A STATION
For a POST request, we have to include the object properties in the request body except the ID. The database generates an ID value when a POST request is successful.