API Debugging: Techniques Beyond console.log
How to inspect requests and responses, interpret status codes and headers, and fix common API integration issues.
APIs are the glue that holds the modern web together. But when they break, they can be incredibly frustrating to debug. A generic 500 Internal Server Error tells you nothing. Let's master professional debugging techniques.
Understanding the HTTP Protocol
To effectively debug APIs, you must understand HTTP fundamentals:
- Methods: GET (read), POST (create), PUT (update full resource), PATCH (update partial), DELETE (remove). Using the wrong method is a common error.
- Headers: Metadata like
Content-Type: application/json,Authorization: Bearer token, orAccept: application/json. Missing or incorrect headers cause 401, 415, or 406 errors. - Body: The actual data payload. Malformed JSON, missing required fields, or wrong types are frequent culprits.
- Status Codes: The server's response indicating success, failure, or redirection.
HTTP Status Codes: Your First Clue
2xx - Success
200 OK- Request succeeded201 Created- Resource successfully created204 No Content- Success but no response body
3xx - Redirection
301 Moved Permanently- Resource has new URL302 Found- Temporary redirect304 Not Modified- Cached version is still valid
4xx - Client Errors (You messed up)
400 Bad Request- Malformed syntax or invalid data401 Unauthorized- Missing or invalid authentication403 Forbidden- Authenticated but lacks permissions404 Not Found- Resource doesn't exist429 Too Many Requests- Rate limit exceeded
5xx - Server Errors (They messed up)
500 Internal Server Error- Generic server failure502 Bad Gateway- Invalid response from upstream server503 Service Unavailable- Server overloaded or down504 Gateway Timeout- Upstream server timed out
Common API Debugging Scenarios
1. CORS Errors
If you see Access-Control-Allow-Origin errors in the browser console, the server isn't allowing requests from your domain. This is a server-side issue, not something you can fix in JavaScript.
Solution: Configure CORS headers on the server or use a proxy during development.
2. Authentication Failures (401)
Check these in order:
- Is the
Authorizationheader present? - Is the token format correct? (Usually
Bearer <token>) - Has the token expired? Decode it to check the
expclaim - Are you sending it with every request that needs it?
3. Payload Validation Errors (400)
The API expects specific data but you're sending something else. Common issues:
- Missing required fields
- Wrong data types (string instead of number)
- Invalid JSON syntax (trailing commas, unquoted keys)
- Exceeding max length or min/max values
4. Unexpected Response Format
Always check the Content-Type response header. If you're expecting JSON but getting HTML (often an error page), parse it as text first to see the actual error message.
Professional Debugging Workflow
- Open DevTools Network Panel: See the actual request/response, not what your code thinks it sent
- Check the Status Code: This narrows down if it's client (4xx) or server (5xx) issue
- Inspect Request Headers: Especially
Content-TypeandAuthorization - Examine Request Payload: Is the JSON valid? Are all required fields present?
- Read Response Body: Many APIs return helpful error messages in the response
- Test in Isolation: Use an API testing tool to rule out frontend code issues
Beyond Browser Tools
While browser DevTools are essential, sometimes you need more control. Standalone API testers let you:
- Craft exact requests without writing code
- Save and reuse common requests
- Test endpoints that aren't called by your UI
- Verify server behavior independent of frontend issues
Request/Response Inspection
Request Headers to Check
- Content-Type: Must match what the API expects (usually
application/json) - Accept: Tells the server what response format you want
- Authorization: Bearer tokens, API keys, or Basic auth credentials
- User-Agent: Some APIs require specific user agents
- X-API-Key: Custom header for API key authentication
Response Headers to Inspect
- Content-Type: Verify you're getting the expected format
- X-RateLimit-Remaining: Check if you're hitting rate limits
- X-Request-ID: Useful for correlating logs on the server side
- Cache-Control: Understand caching behavior
Advanced Debugging Techniques
1. Logging Full Request/Response
Don't just log the response body. Log the entire request (method, URL, headers, body) and response (status, headers, body). This gives you complete context when debugging later.
2. Using Request IDs
Many APIs return a request ID in the response headers. Use this to correlate client-side errors with server-side logs, making it much easier to debug issues with the API provider.
3. Testing with Minimal Payloads
When debugging, start with the simplest possible request. Add complexity incrementally to isolate which part is causing the issue.
4. Comparing Working vs. Broken Requests
If you have a working request and a broken one, compare them side-by-side. Differences in headers, payload structure, or URL parameters will reveal the issue.
Common API Integration Patterns
REST APIs
Most common API style. Uses standard HTTP methods and status codes. Resources are identified by URLs, and operations are performed via HTTP verbs.
GraphQL
Single endpoint that accepts queries. Allows clients to request exactly the data they need. Debugging requires understanding the query structure and schema.
Webhooks
Reverse APIs where the server calls your endpoint. Debugging requires inspecting incoming requests and verifying signatures/authentication.
Test Your APIs Right Now
Our API Tester lets you craft custom HTTP requests directly in your browser. Set methods, headers, and request bodies to isolate issues and debug faster no Postman installation required.
Key features that make debugging easier:
- Support for all HTTP methods (GET, POST, PUT, PATCH, DELETE, etc.)
- Custom header management with common headers pre-filled
- Request body editor with syntax highlighting for JSON
- Full response inspection (status, headers, body, timing)
- Request history to compare different attempts
- No server-side logging - all requests are made directly from your browser
Perfect for quickly testing authentication flows, debugging CORS issues, verifying API responses during development, and learning how different APIs work.
Try these tools
Use these tools alongside this guide
Part of the ThenCatch blog. Learn more about us or browse more guides.