cURL Examples
This page provides comprehensive cURL examples for testing the JSONPlaceholder API from the command line.
Basic Setup
Choose the appropriate base URL for your use case:
bash
# Set base URL for public API
export BASE_URL="https://api.jsonplaceholder.dev"
# Test connection
curl $BASE_URL/
bash
# Set base URL for local development
export BASE_URL="http://localhost:3000"
# Test connection
curl $BASE_URL/
Users Examples
Get All Users
bash
# Simple GET request
curl $BASE_URL/users
# Pretty print JSON output
curl -s $BASE_URL/users | jq '.'
# Show response headers
curl -i $BASE_URL/users
Get User by ID
bash
# Get specific user
curl $BASE_URL/users/1
# Get user with verbose output
curl -v $BASE_URL/users/1
# Save response to file
curl $BASE_URL/users/1 -o user1.json
Get User's Related Data
bash
# Get user's posts
curl $BASE_URL/users/1/posts
# Get user's albums
curl $BASE_URL/users/1/albums
# Get user's todos
curl $BASE_URL/users/1/todos
Create New User
bash
# Create user with minimal data
curl -X POST $BASE_URL/users \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"username": "johndoe",
"email": "[email protected]"
}'
# Create user with full data
curl -X POST $BASE_URL/users \
-H "Content-Type: application/json" \
-d '{
"name": "Jane Smith",
"username": "janesmith",
"email": "[email protected]",
"address": {
"street": "123 Main St",
"suite": "Apt 4B",
"city": "New York",
"zipcode": "10001",
"geo": {
"lat": "40.7128",
"lng": "-74.0060"
}
},
"phone": "1-555-123-4567",
"website": "janesmith.com",
"company": {
"name": "Smith Corp",
"catchPhrase": "Innovation at its best",
"bs": "revolutionary solutions"
}
}'
Update User
bash
# Full update (PUT)
curl -X PUT $BASE_URL/users/1 \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe Updated",
"username": "johndoe",
"email": "[email protected]"
}'
# Partial update (PATCH)
curl -X PATCH $BASE_URL/users/1 \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]"
}'
Delete User
bash
# Delete user
curl -X DELETE $BASE_URL/users/1
# Delete with verbose output
curl -X DELETE -v $BASE_URL/users/1
Posts Examples
Get Posts
bash
# Get all posts
curl $BASE_URL/posts
# Get posts by specific user
curl "$BASE_URL/posts?userId=1"
# Get single post
curl $BASE_URL/posts/1
# Get post comments
curl $BASE_URL/posts/1/comments
Create Post
bash
# Create new post
curl -X POST $BASE_URL/posts \
-H "Content-Type: application/json" \
-d '{
"userId": 1,
"title": "My Amazing Post",
"body": "This is the content of my amazing post. It contains valuable information!"
}'
# Create post with data from file
echo '{
"userId": 1,
"title": "Post from File",
"body": "This post was created from a JSON file."
}' > post.json
curl -X POST $BASE_URL/posts \
-H "Content-Type: application/json" \
-d @post.json
Update Post
bash
# Update entire post
curl -X PUT $BASE_URL/posts/1 \
-H "Content-Type: application/json" \
-d '{
"userId": 1,
"title": "Updated Post Title",
"body": "This is the updated content of the post."
}'
# Update only title
curl -X PATCH $BASE_URL/posts/1 \
-H "Content-Type: application/json" \
-d '{
"title": "New Title Only"
}'
Delete Post
bash
# Delete post
curl -X DELETE $BASE_URL/posts/1
Comments Examples
Get Comments
bash
# Get all comments
curl $BASE_URL/comments
# Get comments for specific post
curl "$BASE_URL/comments?postId=1"
# Get single comment
curl $BASE_URL/comments/1
Create Comment
bash
# Add comment to post
curl -X POST $BASE_URL/comments \
-H "Content-Type: application/json" \
-d '{
"postId": 1,
"name": "Great insights!",
"email": "[email protected]",
"body": "Thanks for sharing this valuable information. It really helped me understand the topic better."
}'
Update Comment
bash
# Update comment
curl -X PATCH $BASE_URL/comments/1 \
-H "Content-Type: application/json" \
-d '{
"body": "Updated comment with new insights."
}'
Albums & Photos Examples
Albums
bash
# Get all albums
curl $BASE_URL/albums
# Get albums by user
curl "$BASE_URL/albums?userId=1"
# Get single album
curl $BASE_URL/albums/1
# Get album photos
curl $BASE_URL/albums/1/photos
# Create album
curl -X POST $BASE_URL/albums \
-H "Content-Type: application/json" \
-d '{
"userId": 1,
"title": "My Vacation Photos"
}'
Photos
bash
# Get photos by album (recommended over getting all 5000 photos)
curl "$BASE_URL/photos?albumId=1"
# Get single photo
curl $BASE_URL/photos/1
# Create photo
curl -X POST $BASE_URL/photos \
-H "Content-Type: application/json" \
-d '{
"albumId": 1,
"title": "Beautiful sunset",
"url": "https://via.placeholder.com/600/sunset",
"thumbnailUrl": "https://via.placeholder.com/150/sunset"
}'
Todos Examples
Get Todos
bash
# Get all todos
curl $BASE_URL/todos
# Get todos by user
curl "$BASE_URL/todos?userId=1"
# Get single todo
curl $BASE_URL/todos/1
Manage Todos
bash
# Create todo
curl -X POST $BASE_URL/todos \
-H "Content-Type: application/json" \
-d '{
"userId": 1,
"title": "Learn cURL commands",
"completed": false
}'
# Mark todo as completed
curl -X PATCH $BASE_URL/todos/1 \
-H "Content-Type: application/json" \
-d '{
"completed": true
}'
# Delete todo
curl -X DELETE $BASE_URL/todos/1
Admin Examples
Reset Data
bash
# Reset all data to initial state (only works when ENABLE_UPDATES=1)
curl -X POST $BASE_URL/reset
# Reset with verbose output to see response
curl -X POST -v $BASE_URL/reset
# Check if reset worked by getting user count
curl -s $BASE_URL/users | jq 'length'
Note: The reset endpoint is only available when the server is running with ENABLE_UPDATES=1
. In read-only mode, this endpoint will return an error.
Advanced cURL Techniques
Authentication Simulation
bash
# Add custom headers (simulating authentication)
curl $BASE_URL/users \
-H "Authorization: Bearer fake-token" \
-H "X-User-ID: 123"
Batch Operations with Shell Scripts
bash
#!/bin/bash
# batch-create-posts.sh
BASE_URL="http://localhost:3000"
posts=(
'{"userId": 1, "title": "Post 1", "body": "Content 1"}'
'{"userId": 1, "title": "Post 2", "body": "Content 2"}'
'{"userId": 2, "title": "Post 3", "body": "Content 3"}'
)
for post in "${posts[@]}"; do
echo "Creating post: $post"
curl -X POST $BASE_URL/posts \
-H "Content-Type: application/json" \
-d "$post"
echo -e "\n---"
done
Response Processing
bash
# Extract specific fields with jq
curl -s $BASE_URL/users/1 | jq '.name'
# Count items
curl -s $BASE_URL/posts | jq 'length'
# Filter data
curl -s "$BASE_URL/todos?userId=1" | jq '[.[] | select(.completed == false)]'
# Get only specific fields
curl -s $BASE_URL/users | jq '.[] | {id, name, email}'
Error Handling
bash
# Check HTTP status code
curl -w "%{http_code}" -s -o /dev/null $BASE_URL/users/999
# Show timing information
curl -w "@curl-format.txt" -s $BASE_URL/users/1
# Create curl-format.txt:
cat > curl-format.txt << 'EOF'
Response Time: %{time_total}s
HTTP Status: %{http_code}
Size: %{size_download} bytes
EOF
Testing Error Scenarios
bash
# Test 404 - Not Found
curl -i $BASE_URL/users/999
# Test invalid JSON (should cause 400 error)
curl -X POST $BASE_URL/posts \
-H "Content-Type: application/json" \
-d '{"invalid": json}'
# Test missing Content-Type header
curl -X POST $BASE_URL/posts \
-d '{"userId": 1, "title": "Test"}'
Performance Testing
bash
# Time multiple requests
time for i in {1..10}; do
curl -s $BASE_URL/users/$i > /dev/null
done
# Parallel requests
curl $BASE_URL/users/1 & \
curl $BASE_URL/users/2 & \
curl $BASE_URL/users/3 & \
wait
# Using curl with URL list
echo -e "$BASE_URL/users/1\n$BASE_URL/users/2\n$BASE_URL/users/3" | \
xargs -I {} -P 3 curl -s {}
Useful cURL Options
bash
# Common useful options:
curl \
-X POST \ # HTTP method
-H "Content-Type: application/json" \ # Headers
-d '{"data": "value"}' \ # Request body
-i \ # Include headers in output
-s \ # Silent mode (no progress bar)
-v \ # Verbose output
-w "%{time_total}" \ # Write custom format
-o output.json \ # Output to file
-L \ # Follow redirects
--max-time 30 \ # Timeout in seconds
--retry 3 \ # Retry on failure
$BASE_URL/endpoint
Environment Setup Script
bash
#!/bin/bash
# setup-api-env.sh
# Set up environment for API testing
export BASE_URL="http://localhost:3000"
export CONTENT_TYPE="Content-Type: application/json"
# Test functions
test_api() {
echo "Testing API connection..."
curl -s $BASE_URL/ | jq -r '.message'
}
get_user() {
curl -s $BASE_URL/users/$1 | jq '.'
}
create_post() {
curl -X POST $BASE_URL/posts \
-H "$CONTENT_TYPE" \
-d "{\"userId\": $1, \"title\": \"$2\", \"body\": \"$3\"}"
}
# Usage examples:
# test_api
# get_user 1
# create_post 1 "My Title" "My Content"