I have a simple app made in Flask. It uses only POST method. It takes 2 numbers (in json) and adds them up.
{"a": 1, "b": 1}
The application responds with the sum of these numbers (also in json).
{"sum": 2}
The code looks like this:
@appFlask.route('sum', methods=['POST'])
def result():
data = request.get_json()
return jsonify({'sum': data['a'] + data['b']})
I would like to do error handling. The problem is that there are a lot of these errors, e.g. for the following calls, they should return "400 Bad Request" or if the file isn't in json.
{"a":1, "b":1, "c":1}
{"a", "a":1, "b":1}
{}
{"a":1}
{"a":0, "b":1}
How can i make it in the simplest way? Is there a way to do this in one function?