Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4066

Python Flask Reqparse failing to return help message on bad request

$
0
0

I am building a simple API and using reqparse to parse the args on a post request as follows:

reservation_post_args = reqparse.RequestParser()reservation_post_args.add_argument('property_id', type=int, help='Property ID is required', required=True)reservation_post_args.add_argument('user_id', type=int, help='User ID is required', required=True)reservation_post_args.add_argument('start_date', type=str, help='Start date is required', required=True)reservation_post_args.add_argument('end_date', type=str, help='End date is required', required=True)@app.route('/make_reservation', methods=['POST'])def make_reservation():    args = reservation_post_args.parse_args()    # do some stuff with the posted reservation...    return jsonify(args), 201

When I test this with a properly formatted set of args, it works fine. For example,

BASE = 'http://127.0.0.1:5000/'response = requests.post(BASE +'make_reservation', json={'user_id':1, 'start_date': '2021-01-01', 'end_date': '2021-01-02'})print(response.json())

prints: {'end_date': '2021-01-02', 'id': 4, 'property_id': 1, 'start_date': '2021-01-01', 'user_id': 1}

When I introduce a faulty set of args -- for instance, leaving out a required arg like property_id -- I expect to see the "help" message as defined in my request parser. However, I end up getting this wonky error because response.json() seems to be raising a decode error.

Traceback (most recent call last):  File "/Users/myname/Library/Python/3.9/lib/python/site-packages/requests/models.py", line 971, in json    return complexjson.loads(self.text, **kwargs)  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 346, in loads    return _default_decoder.decode(s)  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode    obj, end = self.raw_decode(s, idx=_w(s, 0).end())  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode    raise JSONDecodeError("Expecting value", s, err.value) from Nonejson.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)During handling of the above exception, another exception occurred:Traceback (most recent call last):  File "/Users/myname/Documents/sentinel-coding-exercise/test.py", line 7, in <module>    print(response.json())  File "/Users/myname/Library/Python/3.9/lib/python/site-packages/requests/models.py", line 975, in json    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I can see that the parser is returning a 400 response as expected, because when i just try to print(response) I can see <Response [400]>. However, for some reason any issue I introduce to the args causes print(response.json()) to completely crash, rather than just printing the expected error message. What's going on?


Viewing all articles
Browse latest Browse all 4066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>