GET /api/v1/users/42 • HTTP 200 OK
Fetch User Profile by ID (REST Resource Query) — Request & Response Example
Demonstrates the most common RESTful pattern: retrieving a single record from a database collection by providing the unique identifier in the URI path.
1. HTTP Client Request (GET)
GET /api/v1/users/42 HTTP/1.1
{
"Accept": "application/json"
}
2. Server JSON Response (HTTP 200 OK)
{
"id": 42,
"username": "alex_developer",
"email": "alex@example.com",
"full_name": "Alex Rivera",
"role": "Staff Engineer",
"account_status": "active",
"created_at": "2024-03-15T08:30:00Z",
"preferences": {
"theme": "dark",
"notifications_enabled": true
}
}
Copy-Paste Code Snippets (cURL, JavaScript Fetch, Python, Node.js)
curl -X GET "https://cometflow.space/api/v1/users/42" \
-H "Accept: application/json"
fetch('https://cometflow.space/api/v1/users/42')
.then(res => res.json())
.then(user => console.log(user.full_name));
import requests
res = requests.get('https://cometflow.space/api/v1/users/42')
user = res.json()
print(f"User: {user['full_name']} ({user['email']})")
How This API Call Works Step-by-Step
- 1. Path Parameter: The client specifies the identifier 42 in the URL /users/42.
- 2. Database Lookup: The server extracts id=42 and runs a database query (e.g., SELECT * FROM users WHERE id = 42).
- 3. Serialization: If found, the database row is converted into a JSON object.
- 4. HTTP 200 OK: Sent back to the client along with content-type headers.
Backend API Functions: The backend function getUserById(req, res) extracts req.params.id, validates that 42 is an integer, queries the repository, and returns res.status(200).json(user).
Frequently Asked Questions
What happens if user 42 does not exist?
The API server responds with an HTTP status code 404 Not Found and a JSON error body like {"error": "User not found"}.