REST APIs
REST (Representational State Transfer) is an architectural style for building web services. A REST API (Application Programming Interface) allows different software applications to communicate with each other over the internet using standard HTTP methods.
1. How it Works
REST APIs function on a Client-Server model:
- Client: The application (web browser, mobile app) making the request.
- Server: The computer storing the data and logic that processes the request.
The client sends a Request to a specific URL (Endpoint), and the server sends back a Response (usually data in JSON format).
2. HTTP Methods (Verbs)
REST APIs use standard HTTP verbs to perform actions on resources.
- GET: Retrieve data from the server. (e.g., Get a list of users)
- POST: Send data to the server to create a new resource. (e.g., Create a new user)
- PUT: Update an entire existing resource. (e.g., Update a user's full profile)
- PATCH: Update part of an existing resource. (e.g., Update just a user's email)
- DELETE: Remove a resource from the server. (e.g., Delete a user)
3. HTTP Status Codes
The server responds with a numeric code indicating the result.
- 2xx Success:
200 OK: Request succeeded.201 Created: Resource successfully created.
- 4xx Client Error:
400 Bad Request: The server couldn't understand the request.401 Unauthorized: Authentication failed or missing.404 Not Found: The requested resource doesn't exist.
- 5xx Server Error:
500 Internal Server Error: Something went wrong on the server side.
4. Data Format (JSON)
While REST can use XML, JSON (JavaScript Object Notation) is the standard format for modern APIs because it is lightweight and easy to parse.
Request Example
POST /api/users HTTP/1.1
Host: api.example.com
Content-Type: application/json
{
"name": "Alice",
"email": "alice@example.com"
}
Response Example
{
"id": 101,
"name": "Alice",
"email": "alice@example.com",
"createdAt": "2023-10-27T10:00:00Z"
}
programming/asynchronous-programming programming/database-basics