REST API Explained: HTTP Methods, Status Codes, and Practical Examples
APIs are an essential part of modern software development.
Websites, mobile applications, desktop applications, and backend services frequently communicate through APIs.
One widely used approach is REST (Representational State Transfer).
Understanding REST APIs helps developers build applications that communicate with external services and manage data through HTTP.
Let's explore the fundamentals with practical examples.
1. What is a REST API?
A REST API exposes resources through URLs and allows clients to interact with those resources using HTTP methods.
Imagine a task management application.
It might expose these endpoints:
GET /api/tasks
GET /api/tasks/10
POST /api/tasks
PUT /api/tasks/10
PATCH /api/tasks/10
DELETE /api/tasks/10
Each endpoint represents an operation involving task resources.
2. Understand the HTTP methods
GET — Retrieve information
Used to retrieve a resource or collection of resources.
GET /api/tasks
Possible response:
[
{
"id": 1,
"title": "Learn REST APIs",
"completed": false
},
{
"id": 2,
"title": "Build a project",
"completed": true
}
]
POST — Create information
Used to submit data, commonly to create a resource.
POST /api/tasks
Content-Type: application/json
Request body:
{
"title": "Study JavaScript",
"completed": false
}
PUT — Replace a resource
Used to replace the representation of an existing resource.
PUT /api/tasks/1
PATCH — Partially update a resource
Used to update selected fields.
PATCH /api/tasks/1
Content-Type: application/json
Request body:
{
"completed": true
}
DELETE — Remove a resource
Used to request deletion of a resource.
DELETE /api/tasks/1
3. Understand HTTP status codes
HTTP status codes tell clients what happened when a request was processed.
| Status | Meaning |
|---|---|
| 200 | OK |
| 201 | Created |
| 204 | No Content |
| 400 | Bad Request |
| 401 | Unauthorized |
| 403 | Forbidden |
| 404 | Not Found |
| 500 | Internal Server Error |
A common distinction worth remembering:
- 401: Authentication is missing or invalid.
- 403: The server understood the request but refuses access.
Using appropriate status codes makes APIs easier to understand and integrate.
4. Make an API request using JavaScript
The Fetch API allows JavaScript applications to send HTTP requests.
Example:
async function fetchTasks() {
try {
const response = await fetch(
"https://jsonplaceholder.typicode.com/todos"
);
if (!response.ok) {
throw new Error(
`HTTP error: ${response.status}`
);
}
const tasks = await response.json();
console.log(tasks);
} catch (error) {
console.error("Request failed:", error);
}
}
fetchTasks();
This example demonstrates:
- Sending an HTTP request.
- Checking the response status.
- Converting JSON into JavaScript data.
- Handling errors.
The JSONPlaceholder service is a demonstration API, so it is useful for practice rather than production data storage.
5. What is JSON?
JSON stands for JavaScript Object Notation.
It is a lightweight data-interchange format commonly used by APIs.
Example:
{
"username": "developer123",
"active": true,
"skills": [
"Python",
"JavaScript",
"C#"
]
}
JSON supports objects, arrays, strings, numbers, booleans, and null values.
6. Important REST API security practices
When building production APIs:
- Use HTTPS.
- Authenticate requests appropriately.
- Enforce authorization on the server.
- Validate incoming data.
- Apply appropriate rate limits.
- Avoid exposing sensitive information in responses.
- Protect API credentials.
An API should never assume that a request is trustworthy just because it comes from your own frontend.
7. How to practice
You can improve your API skills by building a small project.
Try creating a task management API with:
- An endpoint to retrieve tasks.
- An endpoint to create tasks.
- An endpoint to update task status.
- An endpoint to delete tasks.
- Input validation and error responses.
You can test your endpoints with tools such as Postman or curl.
Final thoughts
REST APIs provide a common way for different applications to communicate.
Understanding HTTP methods, status codes, JSON, and security practices gives you a strong foundation for backend and full-stack development.
Question: Which backend technology do you prefer for building REST APIs: Node.js, ASP.NET Core, Django, Laravel, or something else?
Top comments (0)