Below is a concise cheat sheet for working with REST APIs (Representational State Transfer), covering common HTTP methods, status codes, and request/response formats.
HTTP Methods
- GET:
- Retrieve data from the server.
- Example:
GET /api/users
- POST:
- Send data to the server to create a new resource.
- Example:
POST /api/users
- PUT:
- Update an existing resource on the server.
- Example:
PUT /api/users/123
- PATCH:
- Apply partial modifications to an existing resource.
- Example:
PATCH /api/users/123
- DELETE:
- Remove a resource from the server.
- Example:
DELETE /api/users/123
Status Codes
- 2xx Success:
- 200 OK: Successful GET request.
- 201 Created: Successful POST request, resource created.
- 204 No Content: Successful request, no response body (for DELETE).
- 4xx Client Errors:
- 400 Bad Request: Malformed request or invalid parameters.
- 401 Unauthorized: Authentication required or credentials invalid.
- 403 Forbidden: Authentication successful, but access is not allowed.
- 404 Not Found: Resource not found.
- 5xx Server Errors:
- 500 Internal Server Error: General server error.
- 502 Bad Gateway: Server acting as a gateway received an invalid response.
- 503 Service Unavailable: Server temporarily unable to handle the request.
Request Headers
- Authorization:
- Include authentication credentials (e.g., bearer token).
- Content-Type:
- Specify the format of the request payload (e.g., application/json).
- Accept:
- Specify the expected response format.
Request Parameters
- Query Parameters:
- Included in the URL to filter or paginate results.
- Example:
/api/users?role=admin&page=1
- Path Parameters:
- Included in the URL path to identify a specific resource.
- Example:
/api/users/{userId}
- Request Body:
- Used in POST, PUT, PATCH requests to send data in the request payload.
- Example (JSON):
{ "name": "John", "email": "[email protected]" }
Response Body Formats
- JSON (JavaScript Object Notation):
- Lightweight data interchange format.
- Example:
{
"id": 123,
"name": "John Doe",
"email": "[email protected]"
}
XML (eXtensible Markup Language):
- Hierarchical and extensible markup language.
- Example:
<user>
<id>123</id>
<name>John Doe</name>
<email>[email protected]</email>
</user>
This cheat sheet provides a quick reference for common concepts in working with REST APIs. Always refer to the API documentation provided by the service you are interacting with for specific details and requirements.