DEV Community

Hello world
Hello world

Posted on

šŸ¤” Am I missing something? My Python test passed, but the API payload is weird

Hey everyone! I’m still relatively new to backend development and Python, so please correct me if I'm misunderstanding how this should work.
I was running a quick test script to check a third-party address verification API using the requests library. My test code looked like this:
`import requests

Simple test check

response = requests.get("https://example.com")

if response.ok:
print("🟢 Test passed! The connection is green.")

The script successfully printed 🟢 Test passed! because the network returned a clean 200 OK status code.
However, when I printed the actual JSON response payload, it looked like this:
{
"status": "success",
"data": {
"street": "ERROR: INTERNAL_TIMEOUT_FALLBACK",
"postal_code": "00000"
}
}
`
Even though the server ran into a timeout error internally, it still sent a 200 network status and wrapped the error string inside the data dictionary. Because my script only checked response.ok, it almost saved "ERROR: INTERNAL_TIMEOUT..." into my database as a real street name!
As a beginner, I always assumed that if a server fails to do its job, it throws an error code (like 500 or 400). Is it normal for production APIs to hide errors inside a successful 200 OK response?
I’d love to hear how you senior devs handle or validate data payloads like this in Python to keep your systems safe. Let me know what I might be doing wrong!

Just a note: The code, the bug scenario, and all the beginner questions here are 100% my own actual experience. I just used an AI tool to clean up my bad grammar, fix the layout, and make my thoughts easier to read before posting.

Top comments (0)