RESTful API

RESTful API (Representational State Transfer API) is a method of designing application interfaces based on the REST architectural style, widely used for data exchange between web services and systems. REST was first proposed by Roy Fielding in his 2000 doctoral dissertation, emphasizing principles such as statelessness, client-server separation, uniform interface, and cacheability. The core of RESTful API is to perform create, read, update, and delete (CRUD) operations on resources through the HTTP protocol.

Features of RESTful API

  • Statelessness: Each request contains all necessary information (such as authentication information), and the server does not retain the client's state between requests.
  • Client-Server Architecture: The client is responsible for the user interface and user experience, while the server handles data storage and business logic, with both being independent of each other.
  • Uniform Interface: RESTful API uses standard HTTP verbs (GET, POST, PUT, DELETE, etc.) for operations, with consistent interface design specifications that are easy to understand and use.
  • Cacheability: Response data can be marked as cacheable to improve performance and reduce server load.
  • Layered System: Improves system scalability and security through intermediate layers (such as load balancers and proxy servers).
  • Code on Demand: The server can send code or scripts to the client for execution to enhance client functionality.

HTTP Verbs

  • GET: Used to read resources without affecting any resources on the server.
  • POST: Used to create new resources or submit data; the server creates new resources after processing the request.
  • PUT: Used to update existing resources, replacing server resources with data provided by the client.
  • DELETE: Used to delete resources on the server.
  • PATCH: Used to partially update resources, changing part of the resource's content.

URL Design

RESTful API uses Uniform Resource Identifiers (URI) to represent resources, with each resource having a unique URL. Relationships between resources are represented through URL hierarchy.

Example URL design:

  • GET /books: Get a list of all books.
  • GET /books/{id}: Get a specific book by ID.
  • POST /books: Create a new book.
  • PUT /books/{id}: Update a specific book by ID.
  • DELETE /books/{id}: Delete a specific book by ID.

Response Status Codes

RESTful API uses HTTP status codes to indicate the processing results of requests:

  • 200 OK: Request successful, data returned.
  • 201 Created: Resource created successfully.
  • 204 No Content: Request successful, no content returned.
  • 400 Bad Request: Request is invalid or incorrectly formatted.
  • 401 Unauthorized: Request is unauthorized.
  • 404 Not Found: Requested resource does not exist.
  • 500 Internal Server Error: Server internal error.

Summary

RESTful API is a simple, flexible, and easily extensible web service design method that operates through standard HTTP protocols and verbs, making communication between clients and servers more intuitive and efficient. It is suitable for web applications of various scales, from small projects to large distributed systems.