Key Points and Notes to Master REST API

Key Points and Notes to Master REST API

·

3 min read

Definition of REST API

  • REST API stands for Representational State Transfer Application Programming Interface.

  • It follows certain rules and standards to enable communication between clients and servers.

  • It is a best practice approach for designing APIs.


Understanding the Client-Server Model

  • The client and server are separate entities.

  • Client can be:

    • A browser,

    • A mobile device,

    • A smart device (e.g., Alexa).

  • The server processes requests from the client and sends a response.


How Client and Server Communicate

  1. The client sends a request to the server.

  2. The server processes the request.

  3. The server returns a response.

  4. This communication follows rules and best practices defined by REST API.


Key Principles of REST API

1. Client-Server Architecture

  • The client and server should be independent.

  • The server should not be concerned with the client’s UI or presentation logic.

  • The response should be format-agnostic (i.e., JSON, XML, Text, etc.).

2. Statelessness

  • Each request from a client must contain all necessary information for the server to process it.

  • The server does not store any client-specific session data.

3. Cacheability

  • Responses should be cacheable to improve performance.

  • The server can define whether a response can be cached or not.

4. Uniform Interface

  • The API should have consistent and predictable endpoints.

  • Uses standard HTTP methods:

    • GET - Retrieve data

    • POST - Create new data

    • PUT/PATCH - Update data

    • DELETE - Remove data

5. Layered System

  • The client should not need to know if it is communicating with the final server or an intermediary (proxy, load balancer, etc.).

  • This improves scalability and security.

6. Code on Demand (Optional)

  • Servers can send executable code (like JavaScript) to the client, but this is rarely used.

Data Formats in REST API

  • The server response can be in multiple formats:

    • Plain text

    • Images

    • HTML

    • JSON (JavaScript Object Notation) - Most common

    • XML (Extensible Markup Language)

Why Use JSON Instead of HTML?

  • HTML responses are good for browsers but not suitable for mobile devices or smart devices.

  • JSON is preferred because:

    • It is lightweight and faster.

    • It can be easily parsed by various programming languages.

    • It reduces client-server dependency.


Example of a REST API Request and Response

Request (Client-side)

GET /blogs HTTP/1.1
Host: example.com

Response (Server-side)

{
  "blogs": [
    {
      "id": 1,
      "title": "Understanding REST API",
      "author": "Merlin AI"
    },
    {
      "id": 2,
      "title": "Best Practices in API Design",
      "author": "John Doe"
    }
  ]
}

Conclusion

  • REST API follows a structured approach for client-server communication.

  • It ensures scalability, flexibility, and efficiency.

  • Best practices should always be followed while designing REST APIs.