Search⌘ K

Interactive Testing: Handling Errors

Explore how to perform interactive API testing focusing on error handling and data validation in Flask applications. Learn to identify common issues, use HTTPie for testing, and understand the importance of preparing data before routing to prevent server crashes and improve API reliability.

Issues faced

We use httpie to treat the key-value pairs as form input by including the --form or -f option.

$ http --form POST 0.0.0.0:3000/my-api number=1105 description="First Carmichael number"

We get a return code of 500 and an internal server error. What happened? How do we prevent this? When it happens, we see the error message in the terminal where the server is running:

Assembly (GAS x86)
Traceback (most recent call last):
File "/Users/.../site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/.../site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/.../site-packages/flask_restplus/api.py", line 325, in wrapper
resp = resource(*args, **kwargs)
File "/Users/.../site-packages/flask/views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "/Users/.../site-packages/flask_restplus/resource.py", line 44, in dispatch_request
resp = meth(*args, **kwargs)
File "/Users/.../flask-examples/flask_app.py", line 67, in post
if not ('number' in api.payload and 'description' in api.payload):
TypeError: argument of type 'NoneType' is not iterable

It points us to line 65 in the POST method. We assumed that api.payload would be defined. When no payload is given, api.payload is not an empty dictionary but None. This makes ...