What is REST and what makes an API Restful API

INTRODUCTION

An API (Application Programming Interface) is a mechanism of sharing data between two applications. It allows two different systems built using different technology stacks to communicate with each other.

Whenever you use Facebook or Google maps, the application sends a request to the server over the Internet. The server receives that request performs appropriate actions based on the request and sends back a response. The application interprets the response and presents the information.

A web API also referred to as Web Service, is simply an API for web applications. A web application is one that uses the Internet for communication.

You must have heard of SOAP (Simple Object Access Protocol) that was introduced back in 1998 as a protocol for communication between applications. It was XML based, used WSDL (Web service definition language), and required a SOAP client to make requests.

RPC (Remote procedure call) was also a popular means of communication. RPC calls were used with XML, and with the introduction of JSON in 2002, they could also be used with this new lightweight messaging format. RPC requires the user to remember the procedure name and the order of parameters.

But today, REST is the standard way of developing APIs in the industry. More than 70% of the public APIs available on the Internet are RESTful. So, let’s understand REST.

REST (Representation State Transfer)

REST is an architectural style for developing APIs. It is a set of constraints that any API needs to follow to be called RESTful API. Dr. Roy Fielding defined these constraints in his 2000 doctorate assertion.

REST constraints

A lot of API’s available on the web claims to be REST, but according to Dr. Roy Fielding, every REST API should meet six key constraints:

  • Client-Server
    • This constraint is about the separation of concerns between client and server. You should be able to make changes in each one of them without affecting the other.
  • Stateless
    • Each API call in REST is independent. In other words, each call contains all the necessary information for the server to process that request. A REST API call should not be dependent on sessions or information stored at the server. Each call is self-sufficient. This increases API reliability.
  • Cache
    • The stateless constraint can increase the API request; hence a REST API should be designed to encourage caching of the data. If the response data can be cached, the server should explicitly mention in the response that this response can be cached along with the expiration time.
  • Uniform Interface
    • This constraint is vital for the decoupling of the client from the server. Uniform interface means the client should be able to make a request to the server in an unchanging and standardized way.
  • Layered System
    • This constraint is about the architecture of the system that implies the application should be built using a layered approach where each layer has a specific functionality. It helps in creating a modular and scalable application.
  • Code on Demand
    • The only optional constraint that allows the server to send code as a response to an API call to be used by the system. It helps in creating a flexible application that is not only dependent on its code.

Anatomy of REST APIs

  • Endpoint
    • An endpoint is one end of the communication. It is usually a URL that includes server and resource names where the request is being made. For example, example.com/usersis an endpoint that consists of the server name (www.example.com) and the resource (user).
    • A resource is a fundamental concept in REST API. A resource is any entity/object that has a name and some associated properties. Examples of resources include user, location, box, etc.
  • Method
    • A method specifies the type of request to make. REST uses HTTP verbs as methods such as GET, POST, PUT, DELETE, and PATCH.
    • A GET request to the endpoint (example.com/users) will return the list of all the users.
    • A POST request to the same endpoint will create a new user.
  • Headers
    • Headers provide extra information about the request and response.
    • Request headers provide additional information about the request being made, such as a Content-Type header is used to indicate the type of request body.
    • Response headers provide additional information about the response sent from the server, like the content length or MIME type of the response.

Advantages of REST APIs

  • REST completely separates the user interface from the server and database. You can move the User interface to a new platform without affecting the backend system and, similarly, vice versa.
  • The teams can scale independently, migrate servers, or make any changes without affecting the other components.
  • REST is platform-independent. Your server can be in PHP, Node, or Java. The only thing REST needs are that the response should be exchanged in the decided format like XML or JSON.

CONCLUSION

REST is currently the most popular choice for building APIs, and it is crucial to understand the building blocks to work with Restful services. I hope you now feel more knowledgeable about underlying constraints of REST and what makes an API a Restful API. Happy learning!

Leave a Reply

Your email address will not be published.