API Fundamentals
HTTP Methods
| Method | Purpose | Example |
|---|---|---|
|
Retrieve data |
|
|
Create resource |
|
|
Replace resource |
|
|
Partial update |
|
|
Remove resource |
|
Request Anatomy
GET /api/v1/users?status=active HTTP/1.1
Host: api.example.com
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
Content-Type: application/json
Accept: application/json
{"filter": "active"}
Components:
| Part | Description |
|---|---|
Method + Path |
|
Query Params |
|
Headers |
Metadata (auth, content type) |
Body |
Payload (POST/PUT/PATCH) |
Status Codes
| Code | Category | Meaning |
|---|---|---|
|
Success |
OK - request succeeded |
|
Success |
Created - resource created |
|
Success |
No Content - success, empty response |
|
Client Error |
Bad Request - malformed request |
|
Client Error |
Unauthorized - auth required |
|
Client Error |
Forbidden - auth valid but access denied |
|
Client Error |
Not Found - resource doesn’t exist |
|
Client Error |
Unprocessable - validation failed |
|
Client Error |
Too Many Requests - rate limited |
|
Server Error |
Internal Server Error |
|
Server Error |
Bad Gateway |
|
Server Error |
Service Unavailable |
Common Headers
Request Headers
# Authentication
-H "Authorization: Bearer TOKEN"
-H "X-API-Key: YOUR_KEY"
# Content type (what you're sending)
-H "Content-Type: application/json"
-H "Content-Type: application/x-www-form-urlencoded"
# Accept (what you want back)
-H "Accept: application/json"
# Custom headers (varies by API)
-H "X-Request-ID: uuid-here"
Response Headers
# View response headers
curl -sI https://api.example.com/users
# Common response headers
Content-Type: application/json
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 95
X-Request-Id: abc123
curl Basics
# Silent mode (no progress bar)
curl -s https://api.example.com/data
# Include headers in output
curl -sI https://api.example.com/data
# Verbose (debug)
curl -v https://api.example.com/data
# Follow redirects
curl -sL https://api.example.com/data
# POST with JSON body
curl -s -X POST \
-H "Content-Type: application/json" \
-d '{"name": "test"}' \
https://api.example.com/users
# POST with data from file
curl -s -X POST \
-H "Content-Type: application/json" \
-d @payload.json \
https://api.example.com/users
Practice: JSONPlaceholder
# GET - list users
curl -s https://jsonplaceholder.typicode.com/users | jq
# GET - single user
curl -s https://jsonplaceholder.typicode.com/users/1 | jq
# POST - create (simulated)
curl -s -X POST \
-H "Content-Type: application/json" \
-d '{"title": "foo", "body": "bar", "userId": 1}' \
https://jsonplaceholder.typicode.com/posts | jq
# GET with query params
curl -s "https://jsonplaceholder.typicode.com/posts?userId=1" | jq
Exercises
-
Fetch all posts from JSONPlaceholder, count them with jq
-
Get user 3’s email address only
-
Create a new post, capture the returned ID
-
Fetch posts for user 1, extract just titles