Prompt
How can I debug failed API tests with request and response history?
Latest observation
Nov 9, 2026
OpenAI APIWeb search: off
To debug failed API tests effectively, capture and inspect the full request/response history for each test run. The goal is to see exactly what was sent, what came back, and how it differs from what you expected.
What to record
For every API call, log:
- Request
- HTTP method
- URL/path
- Headers
- Query parameters
- Body/payload
- Response
- Status code
- Headers
- Body
- Response time
- Context
- Test name or ID
- Timestamp
- Environment
- Correlation/request ID if available
How to use it
When a test fails:
-
Compare the actual request to the expected one
- Wrong endpoint?
- Missing auth token?
- Bad header or content type?
- Incorrect JSON shape or field values?
-
Inspect the response
- Status code mismatch (e.g.,
401,403,500) - Error message/body from the API
- Validation errors indicating malformed input
- Status code mismatch (e.g.,
-
Look for patterns across retries or multiple calls
- Did the first request succeed and the second fail?
- Was the failure caused by stale state or dependency setup?
-
Correlate with server logs
- Use request IDs or timestamps to match client-side test history with backend logs.
Practical ways to implement this
- In test frameworks: add hooks/fixtures that save request and response data on failure.
- In API clients: enable verbose/debug logging.
- With proxies/tools: use Postman Console, Charles Proxy, Fiddler, or browser/devtools for capturing traffic.
- In CI: attach logs/artifacts for failed runs so you can review them later.
Example format
{
"test": "Create user returns 201",
"request": {
"method": "POST",
"url": "/users",
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer ***"
},
"body": {
"name": "Alice"
}
},
"response": {
"status": 400,
"headers": {
"Content-Type": "application/json"
},
"body": {
"error": "Missing required field: email"
}
}
}
Best practices
- Redact secrets like tokens, passwords, and PII.
- Store enough history to debug multi-step flows.
- Keep artifacts searchable and tied to the failing test.
- Capture raw payloads, not just parsed objects, when possible.
If you want, I can also show:
- how to do this in pytest, JUnit, or JavaScript/Jest
- a reusable logging middleware/interceptor pattern for API tests