What You'll Learn
- A systematic process for debugging any API response
- What each part of the response tells you
- Common failure patterns and their fixes
- How to use Postman and cURL features for debugging
Why This Matters
Every API call eventually fails. The network drops. The server returns 500. The body is empty. The JSON is malformed. Beginners stare at the error and guess; experienced developers follow a process. This lesson gives you that process.
The 5-Step Debugging Process
When an API call does not behave as expected, follow these steps in order:
Step 1 — Check the status code
The status code tells you which family of problem you're dealing with:
- 2xx — request succeeded; if you're unhappy, your expectation was wrong.
- 3xx — redirect; check if your client follows redirects.
- 4xx — your request is wrong; you need to fix something.
- 5xx — server is broken; not your fault, but you may need to retry.
Step 2 — Read the response headers
Headers often contain the clue:
Content-Type— what format is the body in? If you expected JSON but gottext/html, the server probably returned an error page.WWW-Authenticate— present on 401 responses, tells you which auth scheme to use.Retry-After— present on 429, tells you how long to wait.Location— present on 201 and 3xx, points to the new or moved resource.X-RateLimit-*— many APIs include rate limit info in custom headers.
Step 3 — Read the response body
The body usually has a human-readable error message. Read it carefully — it often says exactly what's wrong:
{
"error": {
"code": "VALIDATION_FAILED",
"message": "The 'email' field must be a valid email address.",
"details": [{"field": "email", "value": "anita@"}]
}
}
This response says: "your email field is invalid". Fix the email and resend.
Step 4 — Compare to the working version
If you have a similar request that works, compare them side by side:
- Same URL pattern?
- Same method?
- Same headers?
- Same body structure?
- Same authentication?
The difference is usually the bug.
Step 5 — Use verbose output
In cURL:
curl -v "https://api.example.com/users"
In Postman: the response panel has a Console button (bottom left). Click it to see the full request and response, including headers and timing.
Common Failure Patterns
Pattern 1 — 401 Unauthorized
What it means: The server did not receive valid authentication.
Debug:
- Is the
Authorizationheader present? - Is the scheme correct? (
Bearer <token>, not just<token>) - Has the token expired? Try refreshing it.
- Are you sending the right token for this environment?
Pattern 2 — 403 Forbidden
What it means: Authentication succeeded, but you don't have permission.
Debug:
- Does your token have the right scopes?
- Are you trying to access another user's resource?
- Does your account have the required plan/role?
Pattern 3 — 404 Not Found
What it means: The URL does not match any resource.
Debug:
- Check the URL spelling — every character matters.
- Is the ID correct? Try GETting the list to find valid IDs.
- Are you using the right base URL for this environment?
- Is the resource deleted? Try a different ID.
Pattern 4 — 400 Bad Request
What it means: The request is malformed.
Debug:
- Is the JSON valid? (Use jsonlint.com to check.)
- Are all required fields present?
- Are field types correct? (String vs number vs boolean.)
- Is the
Content-Typeheader set toapplication/json?
Pattern 5 — 422 Unprocessable Entity
What it means: JSON is valid, but values fail business rules.
Debug:
- Read the error details — they usually name the failing field.
- Is the email format valid?
- Is the date in the expected format (ISO 8601)?
- Are numeric values in the allowed range?
Pattern 6 — 429 Too Many Requests
What it means: You hit the rate limit.
Debug:
- Check the
Retry-Afterheader — that's how long to wait. - Check
X-RateLimit-Remaining— how many requests you have left. - Implement exponential backoff in your client.
- Consider caching responses to reduce request count.
Pattern 7 — 500 Internal Server Error
What it means: The server crashed.
Debug:
- Retry after a few seconds — it may be transient.
- Check the API's status page (e.g., status.github.com).
- If it persists, the request body may have triggered a server bug. Try a minimal body.
- Contact API support with the request ID (often in the response body or headers).
Pattern 8 — 502/503/504
What it means: Network/gateway issues.
Debug:
- 502 Bad Gateway — a proxy got an invalid response. Try again.
- 503 Service Unavailable — server is overloaded or down for maintenance.
- 504 Gateway Timeout — request took too long. Use a longer timeout or paginate.
Pattern 9 — Empty response body
What it means: Either 204 No Content (success), or the server returned nothing.
Debug:
- Check the status code — 204 means success.
- Did you accidentally use HEAD instead of GET?
- Is the response gzip-compressed and your client not decompressing?
Pattern 10 — Connection timeout
What it means: The server did not respond within the timeout.
Debug:
- Is the URL correct? Wrong hostnames cause timeouts.
- Is your network working? Try a known-good site.
- Is the server overloaded? Try at off-peak hours.
- Increase the timeout if the operation is known to be slow.
Using Postman's Console for Debugging
Postman's Console (View → Show Console, or the icon at the bottom left) shows the actual request that was sent and the response that was received. This is invaluable when something goes wrong:
- See the exact URL after variable substitution.
- See all headers that were actually sent.
- See the raw response body before Postman formats it.
- See timing breakdown (DNS, connect, request, response).
Common Mistakes
- Jumping to conclusions. Read the status code and body first. Most errors explain themselves.
- Not comparing to a working request. Side-by-side comparison reveals the bug in seconds.
- Ignoring response headers. The body is shiny, but headers often have the answer.
- Retrying 5xx errors immediately. Use exponential backoff to avoid making the server worse.
- Not checking the API status page. If GitHub is down, no amount of debugging on your side will help.
Practical Exercise (15 minutes)
Intentionally trigger each error against JSONPlaceholder or Open-Meteo and debug it:
- GET a non-existent post ID (expect 404).
- POST with invalid JSON (expect 400).
- GET with a misspelled URL (expect 404).
- PUT to a non-existent resource (expect 404 or 500).
- Send a request with the wrong Content-Type and observe what happens.
For each, follow the 5-step debugging process. Document what you observed at each step.
Mini Challenge
Pick a real API you have access to (or use JSONPlaceholder). Make 5 different intentional mistakes and see if you can debug each one in under a minute using the 5-step process. The goal is to make the process automatic.
Key Takeaways
- 5-step debugging: status code → headers → body → compare to working → verbose output.
- 4xx = your request is wrong; 5xx = server is broken.
- Read the error body carefully — it usually says exactly what's wrong.
- Compare to a similar working request. The difference is the bug.
- Use Postman's Console or cURL's
-vto see the full exchange.
Previously: Lessons 22–24 covered browser, cURL, and Postman.
Today: You learned to debug any API response systematically.
Next: Module 4 is complete. Module 5 begins with lesson 26 — Authentication vs Authorization.
FAQ
What if the API returns 200 but the body is wrong?
This is a server bug or a misunderstanding. Compare the response to what the docs promise. If the docs say field X should be present and it isn't, contact API support or check if you're using the right endpoint/version.
How do I debug a CORS error?
CORS errors only happen in browsers. Check the browser console — it tells you which origin was blocked. If you control the API, add the right Access-Control-Allow-Origin header. If you don't, use a server-side proxy or test with cURL (which ignores CORS).
Comments
Comments
Post a Comment