Meta Description: Debug REST APIs effectively with Postman, cURL, HTTPie, and browser DevTools. Learn request inspection, response analysis, and troubleshooting techniques.
Keywords: api debugging, postman, curl, api testing tools, rest api debugging, api troubleshooting, httpie
Word Count: ~2,200 words
Your API returns an error. But why? Is it the request? The authentication? The server?
Good debugging tools help you find the problem fast. Here are the essential tools for API debugging.
cURL: The Universal Tool
cURL is available everywhere. It's the first tool you reach for.
Basic Requests
# GET request
curl https://api.petstoreapi.com/v1/pets
# POST request
curl -X POST https://api.petstoreapi.com/v1/pets \
-H "Content-Type: application/json" \
-d '{"name":"Max","species":"DOG"}'
# PUT request
curl -X PUT https://api.petstoreapi.com/v1/pets/123 \
-H "Content-Type: application/json" \
-d '{"name":"Max","species":"DOG","status":"ADOPTED"}'
# DELETE request
curl -X DELETE https://api.petstoreapi.com/v1/pets/123
Authentication
# Bearer token
curl https://api.petstoreapi.com/v1/pets \
-H "Authorization: Bearer sk_live_abc123..."
# Basic auth
curl https://api.petstoreapi.com/v1/pets \
-u username:password
# API key in header
curl https://api.petstoreapi.com/v1/pets \
-H "X-API-Key: abc123..."
Debugging Options
# Show response headers
curl -i https://api.petstoreapi.com/v1/pets
# Show request and response headers
curl -v https://api.petstoreapi.com/v1/pets
# Show only headers (no body)
curl -I https://api.petstoreapi.com/v1/pets
# Follow redirects
curl -L https://api.petstoreapi.com/v1/pets
# Save response to file
curl https://api.petstoreapi.com/v1/pets -o pets.json
# Show timing information
curl -w "\nTime: %{time_total}s\n" https://api.petstoreapi.com/v1/pets
Advanced Usage
# Send JSON from file
curl -X POST https://api.petstoreapi.com/v1/pets \
-H "Content-Type: application/json" \
-d @pet.json
# Upload file
curl -X POST https://api.petstoreapi.com/v1/pets/123/photo \
-F "photo=@dog.jpg"
# Custom headers
curl https://api.petstoreapi.com/v1/pets \
-H "Accept: application/json" \
-H "User-Agent: MyApp/1.0" \
-H "X-Request-ID: abc-123"
# Ignore SSL certificate errors (development only!)
curl -k https://localhost:3000/v1/pets
Pretty Print JSON
# Using jq
curl https://api.petstoreapi.com/v1/pets | jq
# Using python
curl https://api.petstoreapi.com/v1/pets | python -m json.tool
# Save formatted JSON
curl https://api.petstoreapi.com/v1/pets | jq > pets.json
HTTPie: cURL for Humans
HTTPie has simpler syntax and better output formatting.
Installation
# macOS
brew install httpie
# Ubuntu/Debian
apt install httpie
# pip
pip install httpie
Basic Usage
# GET request (http is the command)
http https://api.petstoreapi.com/v1/pets
# POST request with JSON
http POST https://api.petstoreapi.com/v1/pets \
name=Max species=DOG breed="Golden Retriever"
# PUT request
http PUT https://api.petstoreapi.com/v1/pets/123 \
name=Max status=ADOPTED
# DELETE request
http DELETE https://api.petstoreapi.com/v1/pets/123
Authentication
# Bearer token
http https://api.petstoreapi.com/v1/pets \
Authorization:"Bearer sk_live_abc123..."
# Basic auth
http -a username:password https://api.petstoreapi.com/v1/pets
# Custom header
http https://api.petstoreapi.com/v1/pets \
X-API-Key:abc123...
Output Options
# Show headers only
http --headers https://api.petstoreapi.com/v1/pets
# Show body only
http --body https://api.petstoreapi.com/v1/pets
# Verbose output
http -v https://api.petstoreapi.com/v1/pets
# Download to file
http --download https://api.petstoreapi.com/v1/pets/123/photo
Why HTTPie is Better
Simpler syntax:
# cURL
curl -X POST https://api.petstoreapi.com/v1/pets \
-H "Content-Type: application/json" \
-d '{"name":"Max","species":"DOG"}'
# HTTPie
http POST https://api.petstoreapi.com/v1/pets name=Max species=DOG
Colored output: Syntax highlighting for JSON responses
Automatic JSON: No need to specify Content-Type for JSON
Better error messages: Clear, readable error output
Postman: The Visual Tool
Postman provides a GUI for API testing and debugging.
Key Features
Collections: Organize requests into folders Environments: Switch between dev, staging, production Variables: Reuse values across requests Tests: Write JavaScript assertions Mock servers: Test without a backend Documentation: Auto-generate API docs
Creating a Request
- Click "New" → "Request"
- Enter URL:
https://api.petstoreapi.com/v1/pets - Select method: GET, POST, PUT, DELETE
- Add headers in "Headers" tab
- Add body in "Body" tab (for POST/PUT)
- Click "Send"
Using Variables
// Environment variables
{{baseUrl}}/v1/pets
{{apiKey}}
// Collection variables
{{petId}}
{{userId}}
Set variables in: - Environment settings - Collection settings - Pre-request scripts
Pre-Request Scripts
Run JavaScript before sending the request:
// Generate timestamp
pm.environment.set("timestamp", Date.now());
// Generate random ID
pm.environment.set("requestId", pm.variables.replaceIn('{{$randomUUID}}'));
// Sign request
const crypto = require('crypto-js');
const signature = crypto.HmacSHA256(pm.request.url, pm.environment.get("secret"));
pm.environment.set("signature", signature.toString());
Tests (Assertions)
Run JavaScript after receiving the response:
// Check status code
pm.test("Status is 200", () => {
pm.response.to.have.status(200);
});
// Check response time
pm.test("Response time < 500ms", () => {
pm.expect(pm.response.responseTime).to.be.below(500);
});
// Check JSON structure
pm.test("Response has data array", () => {
const json = pm.response.json();
pm.expect(json).to.have.property('data');
pm.expect(json.data).to.be.an('array');
});
// Save value for next request
pm.test("Save pet ID", () => {
const json = pm.response.json();
pm.environment.set("petId", json.id);
});
Collection Runner
Run multiple requests in sequence:
- Create collection with requests
- Click "Runner"
- Select collection
- Set iterations
- Click "Run"
Use...[truncated 5086 chars]