RESTful API(s)
REST, which stands for Representational State Transfer, is an architectural style or set of principles for designing networked applications. REST has become the predominant way of designing APIs (Application Programming Interfaces) for web-based applications.
REST uses standard HTTP methods. Any client (web, mobile, IoT) capable of making HTTP requests can avail itself of the REST interface. Conversely, REST is supported by most programming languages and frameworks, which makes it a go-to choice for API development.
RESTful API Key Concepts
- Resource-Oriented
-
In REST, every piece of data or functionality is treated as a resource, identified by a unique URI (Uniform Resource Identifier).
Examples are /users, /products, and /orders.
- Client–Server Architecture
-
REST separates the client (the application making the request) from the server (the application fulfilling the request), which allows them to evolve independently.
- Stateless Communication
-
Each API request contains all necessary information (authentication, state, etc.).
The server does not store session information about clients, ensuring scalability.
- HTTP Methods/Verbs: RESTful APIs rely on standard HTTP methods to perform operations on resources
-
Methods or verbs define the type of operation to perform on a given resource. They provide a standardized way to perform actions on resources, ensuring clear communication between clients (e.g., web apps, mobile apps) and servers.
GET
: make a read-only request to view either a single or list of multiple resources.POST
: create a new resource based on the payload given in the body of the requestPUT
: Update the entire fields of the resource based on the given body of the request or create a new one if it does not already existDELETE
: Destroy the given resource based on the ID providedPATCH: Partially update a resource
- Representation Formats
-
RESTful APIs commonly use JSON (JavaScript Object Notation) and XML to represent data.
JSON is preferred due to its simplicity and compatibility with modern web technologies.
- Uniform Interface
- REST enforces a standardized interface, ensuring consistent interaction between clients and servers
HTTP Request Status Codes
When we receive an HTTP request in the basic RESTful format, the server will return an HTTP status code and any optional JSON payloads.
RESTful APIs Core Components
Core Components of RESTful APIs include the following:
- Endpoints: URLs that represent resources, e.g., https://api.example.com/users.
- HTTP Methods: Specify the action to perform on a resource.
- Headers: Provide metadata for requests and responses (e.g., authentication tokens, content types).
- Request Body: Contains data for POST, PUT, or PATCH requests (usually in JSON or XML format).
- Response Body: Contains the server's response, often including data or status messages.
- Status Codes: HTTP status codes inform clients of the result of their request (as explained previously).