What is REST and what are RESTful services?

What is REST and what are RESTful services?

Michael Asaad's photo
Michael Asaad
·Nov 15, 2021·

5 min read

Subscribe to my newsletter and never miss my upcoming articles

Table of contents

  • REST API design
  • What does it mean when we say a REST API is Stateless?
  • What is an HTTP protocol?

I'm sure we've all heard this term thrown around a lot, but what does REST mean and what is a RESTful service? Let's dive in together.

REST is short for Representational State Transfer. It is the process by which a client-side application (your frontend) interacts with a web service (the backend) using HTTP protocols to create, remove, update and delete data (CRUD operations). RESTful services implement a REST design and expose these CRUD operations at specific endpoints, each one dealing with a specific collection of data.

The term REST was introduced by Roy Fielding in his doctoral dissertation in 2000.

An API is an application programming interface. It is a way for the client to talk to another programme or set of programmes.

For example, let's say you are searching for something online on a service like YouTube. You type in your search term "funny cat videos" and hit enter. What you get back is a list of videos that match the search term you put in. That is all done via an API.

A REST API works in the same way. You send a request to your web service with some identifying information (your search term) and it gives you back a list of results. The only difference is the rules you as the developer would follow when creating your API.

For example, in a REST API, you should be able to retrieve some data (a resource) when you call a specific URL. This call should return you some data (a response) which can then be used.

Let's dive a bit deeper into what a REST API is, how you can make one, and what is the best practices to follow.

REST API design

When developing a RESTful service the implementation of the front end and the back and service are done independently. This helps us work towards a practice called separation of concerns. The benefit of using separation of concerns is that the front end, or the client-side, can be updated and changed without affecting the web service. Similarly, the web service logic can also be updated and changed without affecting the client, so long as the endpoints remain unchanged.

In this way, we improve the flexibility of the application as a whole as both the front end and backend are independent of each other. This improves flexibility and scalability.

Using a REST service also means that several clients can feed into that same API and utilise the same exposed endpoints. Makes code more reusable, an important concept in the OOP and the Dry Principle.

Coupling between the client and the web service should be as loosely coupled as possible in order to facilitate large scale designs. Defined resources and entities are used, while the implementation details her hidden. Clients can only access these resources using the URIs made available.

When creating a RESTful API, a base URI will be made available e.g. api.example.com and all the different HTTP methods will work off of that.

What does it mean when we say a REST API is Stateless?

When an API is said to be stateless that means that each call to the API can be made independently of one another. When a call is made to a stateless API endpoint, all the data needed to complete the call is passed along with it.

We can reduce memory requirements of the REST API and keep the application as scalable as possible by storing identifiable information of the state of the client-side. This can also improve the reliability of the API as it means we do not need to rely on the server state to create objects for us as these may sometimes fail. Instead, each call to the REST API should have all the necessary data. An example of identifiable data that could be passed along is access tokens and IDs etc.

RESTful web APIs are normally based on HTTP methods that can be accessed via specific URLs. These URLs are typically encoded and require some sort of authentication or token verification to facilitate access.

Typically JSON or XML would be used to transfer data to and from these URLs and a payload/response is passed back also in the format of JSON or XML.

What is an HTTP protocol?

HTTP is the most common protocol for requests and responses to a RESTful web API.

Some common methods or operations that can be performed via HTTP are GET, POST, PUT and DELETE. Using these methods you can carry out all the basic CRUD (Create, Read, Update, Delete) operations.

GET: used to get a representation of the target resource.

POST: used to process the data in the request.

PUT: used to create or update the state of target resources defined in the request data.

DELETE: used to delete the target resource.

A web server will receive these HTTP requests, carry out the specific action and return a response along with a status code.

Common HTTP status codes

  • 1xxs: Informational responses, the server is still processing the request.
  • 2xxs: Success, the request was successfully completed
  • 3xxs: Redirected, the request was received has redirected you to another address.
  • 4xxs: Client errors, for example, 404 - page not found. The request is made, but the page trying to load is not valid
  • 5xxs: Server error, request failed. A valid request was made but failed to be compeleted at the server.

Did you find this article valuable?

Support Michael Asaad by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
Share this