REST API : First Step towards Foundation of Web Development

Sonali Sahi
4 min readOct 2, 2023

--

Introduction

In the dynamic world of web development, REST APIs (Representational State Transfer Application Programming Interfaces) have emerged as the bedrock of communication between applications and services. REST APIs enable developers to create powerful, scalable, and interoperable systems that can serve a multitude of purposes. In this blog, we’ll delve into the fundamentals of REST APIs, exploring what they are, how they work, and why they are essential in modern web development.

What is a REST API?

REST, short for Representational State Transfer, is a set of architectural principles that provide a standardized way for applications to communicate over the internet.

REST is not a standard or a specific technology; rather, it’s a set of guidelines for creating efficient and scalable web services.

A Web API (or Web Service) conforming to the REST architectural style is a REST API.

Guiding Principles of REST

1. Uniform Interface :

REST APIs should have a consistent and standardized interface. This means using standard HTTP methods and status codes (e.g., 200 OK, 404 Not Found) and having a clear and predictable URL structure.

2. Client-Server:

REST follows a client-server architecture, where the client and server are separate entities that communicate over a network.

This pattern enforces the separation of concerns, which helps the client and the server components evolve independently. By separating the user interface concerns (client) from the data storage concerns (server), we improve the portability of the user interface across multiple platforms and improve scalability by simplifying the server components.

3. Stateless Communication :

Each request from the client to the server must contain all the information needed for the server to understand and process the request. The server does not store client state.

4. Cacheable:

The cacheable constraint requires that a response should implicitly or explicitly label itself as cacheable or non-cacheable.

If the response is cacheable, the client application gets the right to reuse the response data later for equivalent requests and a specified period.

5. Layered System:

REST allows for a layered architecture, where a client may interact with a server through intermediary components like proxies, load balancers, or caches without knowing about them.

6. Code on Demand (Optional):

REST also allows client functionality to extend by downloading and executing code in the form of applets or scripts.

In REST, everything is about resources.

Let’s talk a little bit on REST resources :

1. Resource Identifiers:

Each resource in a RESTful system is uniquely identified by a URI (Uniform Resource Identifier), which serves as its address. URIs provide a consistent and standardized way to locate and access resources.

2. Representation:

Resources can have multiple representations, such as JSON, XML, HTML, or others. Clients can specify their preferred representation using the “Accept” header in the HTTP request. The server responds with the requested representation, promoting flexibility and compatibility between clients and servers.

3. Hypermedia:

The data format of a representation is known as a media type. The media type identifies a specification that defines how a representation is to be processed.

4. Resource Methods :

Resource methods are used to perform the desired transition between two states of any resource.

The most commonly used HTTP methods in REST are:

  • GET: Retrieve data from a resource.
  • POST: Create a new resource.
  • PUT: Update an existing resource (or create it if it doesn’t exist).
  • DELETE: Remove a resource.
  • PATCH: Partially update a resource.
  • OPTIONS: Retrieve information about the communication options for a resource.
  • HEAD: Retrieve metadata about a resource (without its actual data).

The use of these standard methods provides a uniform and intuitive way to interact with resources.

5. Metadata:

Metadata about the resource is made available and used to control caching, detect transmission errors, negotiate the appropriate representation format, and perform authentication or access control.

6. Self-Descriptive Messages:

Responses from the server should include information that allows the client to understand how to process the data. This can include standardized response codes (HTTP status codes) and metadata, such as “Content-Type” headers, to indicate the format of the response.

How Does a REST API Work?

To understand how REST APIs work, let’s walk through a typical scenario:

  1. Client Sends a Request: A client, such as a web application, mobile app, or another service, initiates communication by sending an HTTP request to a specific URI.
  2. Server Processes the Request: The server, equipped with a REST API, receives the request and identifies the requested resource and the requested action (HTTP method).
  3. Data Manipulation: Depending on the HTTP method, the server performs the appropriate action on the resource, like retrieving, updating, creating, or deleting data.
  4. Response Generation: The server then generates an HTTP response, which includes the requested data in the specified format (JSON, XML, etc.). It also includes a status code indicating the success or failure of the operation.
  5. Client Consumes the Response: The client receives the response and processes it as needed. It can display the data to the user, store it locally, or use it for further operations.

References : Rest Arch Style

--

--