Post

REST API

Introduction to REST API

  • REST (Representational State Transfer) API is a set of architectural principles that define how web services can be designed and interacted with.
  • Here are some key points about REST APIs:
  1. Resource-Based:
    • In REST, everything is a resource. Each resource is identified by a specific URL, and you interact with it using standard HTTP methods, such as GET, POST, PUT, DELETE.
  2. Stateless:
    • Each request from a client to a server must contain all the information needed to understand and process the request.
    • The server should not store anything about the latest HTTP request the client made.
    • Each request is treated independently.
  3. Client-Server Architecture:
    • The client and the server are separate entities that communicate over HTTP.
    • The client is responsible for the user interface and user experience, and the server is responsible for processing requests, performing operations, and storing data.
  4. Cacheable:
    • REST APIs can be designed to store cacheable data on the client-side, reducing the number of interactions needed with the server, improving performance.
  5. Uniform Interface:
    • REST APIs have a standard set of methods and a predefined set of stateless operations.

Request Components

  1. HTTP Method:
    • The type of request being made. The most common methods are GET (retrieve data), POST (send data), PUT (update data), DELETE (remove data).
  2. URL (Uniform Resource Locator):
    • The location of the resource on the server that the client wants to interact with.
  3. Headers:
    • Additional information that the client may send to the server, such as the content type of the data being sent, authentication tokens, information about the client making the request, etc.
  4. Body:
    • The actual data being sent to the server. This is typically used in POST and PUT requests where the client is sending data to the server to be created or updated. The body is usually in JSON format in a REST API.

5.Query Parameters:

  • These are optional and used to filter or modify the response. They are included in the URL after a ‘?’ symbol.

Response Components

  1. Status Code:
    • This is a numerical code indicating the result of the server’s attempt to understand and process the request.
    • Examples include 200 (OK), 404 (Not Found), 500 (Internal Server Error), etc.
  2. Headers:
    • These provide additional information about the response or the requested resource.
    • They might include fields like Content-Type (the format of the data in the body), Content-Length, Authorization (for authentication-related data), etc.
  3. Body:
    • This is the actual data that the server sends back to the client.
    • In a REST API, this is typically in JSON format. The body contains the requested resource or the information that the client needs.
  4. Status Message:
    • A brief description of the status code.
    • For example, for a 200 status code, the status message is “OK”.
This post is licensed under CC BY 4.0 by the author.