What is RESTful API

If you have landed on this page, there is a high chance you want to know more about RESTful API. But before we start talking about RESTful API lets define first what is REST API? and What is the difference between REST and RESTful API?.

Related Articles:

What is REST API?

REST, stands for REpresentational State Transfer, is an architectural style that provides standards and a set of rules that systems on the web follow. This standards make the communication between systems much easier and more flexible.  In the REST architectural style, data and functionality are considered resources. Resources are accessed using Uniform Resource Identifiers (URIs), so a resource could be any information: a document or image, a temporal service or a collection of other resources. When an application requests a resource using the resource identifier, the API delivers the current representation of that resource in a format that the client application can consume, such as a JPEG image, HTML page, or JSON.

What is RESTful API?

To be referred as RESTful, a REST API must satisfy the principles illustrated in the table below:

PrinciplesMeaning
Separation of Client and ServerThat means the implementation of the client interface and the implementation of the server and data storage should be done independently without each knowing about the other. This separation will improve the portability of the front end part across multiple platforms and improve scalability by simplifying the server components.
StatelessThe server does not need to know anything about what state the client is in to understand the request and the client can understand any message received, even without seeing previous messages.
CacheableRESTful API requires that the data within a response to be implicitly or explicitly and should be labeled as cacheable or non-cacheable.
Uniform interfaceIn order to obtain a uniform interface, the resources in the system must meet these criteria:
– A resource should have only one logical URI, and that should provide a way to fetch related or additional data. 
– Any single resource should not be too large and contain each and everything in its representation.
– the resource representations should follow specific guidelines such as naming conventions, link formats, or data format (XML or/and JSON).
– All resources should be accessible through a common approach such as HTTP GET and similarly modified using a consistent approach.
Layered systemREST allows the use of a layered system architecture so you can deploy the APIs on server, and store your data on a different server without letting the client Know if he is connected directly to the end server or there is an intermediary along the way.

REST Architecture

REST Architecture

The above architecture shows the communication schema between client and server in a REST API. A request is made by the client and sent to the server in order to create, retrieve, change or delete resources. The server executes the queries related to the client request and sends a response. So let’s take a look at the standard ways to make requests and send responses.

REST Client request

Using REST principles more strictly in the web intends to make it more streamline and standard. So that allows clients and servers exchange representations of resources by using a standardized interface and protocol (typically HTTP or HTTPS for a secure communication) and enables developers to develop any kind of web application having all CRUD paradigm (Create, Retrieve, Update, Delete) operations using HTTP methods (POST, GET, PUT, PATCH, DELETE). So we can say any interface, including HTTP (or HTTPS) and honoring the principals mentioned above can be a RESTful interface.

In REST, a client request is made up of four parts:

  • HTTP method: The method provides the meaning for the request you send to the server and define what operation to perform. HTTP has five types of method that you can use in your API:
MethodMeaning and operation to perform
GETRetrieves a specific resource (or a collection of resources) without making any change to the resource state.
POSTThis method is used to create a new resource into the collection of resources.
PUTUpdates or replaces a specific existing resource identified by the request URI.
PATCHMakes a partial update or a small correction on an existing resource (note that : not all browsers support PATCH method).
DELETEIs used to delete a specific resource.
  • Header: Headers are used to pass informations sent by the client to server. It can be used for authentication and providing information about the body content such as content-type to ensure that the server will not send data that cannot be understood by the client (note that JSON is the common format for sending and requesting data through a REST API). An HTTP request could have different headers under the form of property-value pairs separated by a colon.
  • Path: The path contains the information necessary to determine the resource you are requesting. For example, lets say the starting point for your API is: “https://yourAPIDomaineName.com” and you want to get and retrieve an employee with a specific id, so you navigate to “https://yourAPIDomaineName.com/employees/:id”, the path to the resource, in this case, is “/employees/: id”.
  • Body: This option is only used with HTTP methods and contains data or message.

REST Server response

Responses from the server contain a content types and a response code.

Content type

The content-type is included in response header when the server is sending back data to the client. The type of the content in the response should be one of the options that the client specified in the content-type of request header .

Response code

Responses status codes are used to alert and inform the client about the success of the operation. the table below represents the HTTP methods and their expected status codes and how they are used:

HTTP MethodMeaningStatus codeResponse body
GETRetrieves a specific resource or a collection of resources200 (OK): if the resource is found.
404 (NOT FOUND): if the resource not found.
400 (BAD REQUEST): if the request is not correctly formed.
YES: XML or JSON content
POSTCreate a new resource201 (CREATED)
200 (OK) or 204 (NO CONTENT): Response for successful POST, where nothing is being returned in the response body.
400 (BAD REQUEST): if the request is not correctly formed.
YES: XML or JSON
content with the status code 201
PUT
Update a specific existing resource201 (CREATED): if the resource does not exist and a new one has been created by the PUT.
200 (OK) or 204 (NO CONTENT): Response for successful PUT, where nothing is being returned in the response body.
400 (BAD REQUEST): if the request is not correctly formed.
YES: XML or JSON
content with the status code 201
DELETEDelete a specific resource200 (OK): if the response includes an entity describing the status.
202(ACCEPTED: if the action has been queued.
204 (NO CONTENT): Response for successful PUT, where nothing is being returned in the response body.
404 (NOT FOUND): if the resource was already removed.
NO

Summary

In this article, we answered the questions of what is a REST API and what we call RESTful API. Also, we explained the REST API architecture and gave you the structure of a client request and server response. Hope the table summary was a help to learn more about HTTP methods and responses status codes.

To see a real practice of all the knowledge learnt in this article click here: Building a complete RESTful API with node.js.

You might also like:

Building a complete RESTful API with node.js.

Example How to use initialize() Function in Node.js/Express API .

Why is Connection Pooling better than Single Connection?.

How to create MySQL database using node.js.

Complete JWT Authentication and Authorization System for MySQL/Node.js API.

How to Build a Complete API for User Login and Authentication using MySQL and Node.js.

Node.js + MySQL : Add Forgot/Reset Password to Login-Authentication System.

Nodemailer + Gmail: How to Send Emails from Node.js API using Gmail.

How to store Session in MySQL Database using express-mysql-session.

How to get Request Origin in Express.js?

How to get the Request Host in express.js?

How to Get Current Date and Time in JavaScript.

How to add Custom Error Handler Middleware to your Node.js/Express API.

How to interact with MySQL database using async/await promises in node.js ?

How to use Sequelize async/await to interact with MySQL database in Node.js.

MANY-TO-MANY Association in MYSQL Database using Sequelize async/await with Node.js.

ONE-TO-ONE Association in MYSQL Database using Sequelize async/await with Node.js

ONE-TO-ONE Association in MYSQL Database using Sequelize async/await with Node.js.

How to add Routes to insert data into MySQL database-related tables in Node.js API?

3 easy steps to create React application.

How to create React Web App with a Node.js API Backend.

How to make Axios Requests in React to a nodejs API.

How to use React Hooks in Functional Components.

5 Steps to Set up React Development Environment.

How to clone GitHub repository and branches.

What are Component Lifecycle Methods in React.

How to open Visual Studio code from Command Line.

How to deploy React app to Heroku.

Complete Guide on how to use Git and GitHub.

Translate »