You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using webargs with aiohttp.
When the incoming request has header Content-Type: application/json and has no body, the server returns 500 error.
Below is the error log.
[2018-05-25 16:38:46,436] ERROR Error handling request
Traceback (most recent call last):
File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/site-packages/aiohttp/web_protocol.py", line 381, in start
resp = await self._request_handler(request)
File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/site-packages/aiohttp/web_app.py", line 322, in _handle
resp = await handler(request)
File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/site-packages/aiohttp/web_middlewares.py", line 88, in impl
return await handler(request)
File "/Users/explosic4/qb/ot-rpc/warehouse/utils.py", line 76, in handler_middleware
args = await parser.parse(args, request)
File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/site-packages/webargs/async.py", line 71, in parse
parsed = yield from self._parse_request(schema=schema, req=req, locations=locations)
File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/site-packages/webargs/async.py", line 45, in _parse_request
parsed_value = yield from self.parse_arg(argname, field_obj, req, locations)
File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/site-packages/webargs/async.py", line 102, in parse_arg
value = yield from self._get_value(name, field, req=req, location=location)
File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/site-packages/webargs/async.py", line 119, in _get_value
value = yield from function(req, name, argobj)
File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/site-packages/webargs/aiohttpparser.py", line 102, in parse_json
self._cache['json'] = json_data = yield from req.json()
File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/site-packages/aiohttp/web_request.py", line 537, in json
return loads(body)
File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
The text was updated successfully, but these errors were encountered:
Looks like a possible bug in aiohttp. I would expect yield from req.json() to return either None or raise a more specific error. I suggest reporting this issue on their issue tracker.
For now, webargs should probably handle this case. PRs welcome!
Falcon has a similar behavior because it also passes the body directly to json.dumps and when that raises an error, it's internal media handling raises a bad request or something similar. falconry/falcon#1216
I think this can be fixed by catching the JSONDecodeError and inspecting the exception object's doc member. I submitted PR #297 illustrating this workaround.
I am using webargs with aiohttp.
When the incoming request has header
Content-Type: application/json
and has no body, the server returns 500 error.Below is the error log.
The text was updated successfully, but these errors were encountered: