REST API

Vijayasankar Balasubramanian
2 min readJan 11, 2025

--

A REST API (Representational State Transfer Application Programming Interface) facilitates communication between various software applications over the internet by adhering to a standardized protocol. Below is a practical demonstration of how a REST API operates:

Client Sends a Request

The client (such as a web browser, mobile app, or software application) sends an HTTP request to the server. This request contains:

URL: Identifies the resource (e.g., https://api.example.com/users).

HTTP Method: Specifies the action to perform, such as:

GET – Retrieve data.

POST – Create new data.

PUT – Update existing data.

DELETE – Remove data.

Headers: Additional information (e.g., authentication tokens or content type).

• Body (optional): Data sent with POST or PUT requests.

Server Processes the Request

The server receives the request and processes it by:

1. Routing: Identifying the requested resource based on the URL.

2. Logic Execution: Performing the requested action (e.g., querying a database, creating, updating, or deleting data).

3. Response Preparation: Formatting the response, typically in JSON or XML.

Server Sends a Response

The server sends back an HTTP response containing:

Status Code: Indicates the result of the request (e.g.):

200 OK – Successful request.

201 Created – Resource created.

400 Bad Request – Invalid input.

404 Not Found – Resource not found.

500 Internal Server Error – Server issue.

Headers: Metadata about the response (e.g., content type, cache settings).

Body: Contains the requested data or information about the result of the operation (e.g., user details, error messages).

Client Consumes the Response

The client receives the response and uses the data. For example:

• A web app may display the information to the user.

• A mobile app may process the data further for offline storage.

Example Workflow

Scenario: A user wants to see their profile information in a mobile app.

1. Client Request:

GET /users/123 HTTP/1.1
Host: api.example.com
Authorization: Bearer <token>

2. Server Logic:

• Locate the user in the database with ID 123.

• Fetch the data (e.g., name, email, profile picture).

3. Server Response:

HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 123,
"name": "John Doe",
"email": "john.doe@example.com",
"profilePicture": "https://example.com/images/johndoe.jpg"
}

4. Client Usage:

The mobile app displays “John Doe’s” profile page using the response data.

This interaction exemplifies the stateless nature of REST: each request is independent, and the server doesn’t retain session information between requests.

--

--

Vijayasankar Balasubramanian
Vijayasankar Balasubramanian

Written by Vijayasankar Balasubramanian

Java Solution Architect, Java Full Stack Engineer

No responses yet