Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

500 when content-type is 'application/json' and body is None #229

explosic4 opened this issue May 25, 2018 · 4 comments


Copy link

commented May 25, 2018

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/", line 381, in start
    resp = await self._request_handler(request)
  File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/site-packages/aiohttp/", line 322, in _handle
    resp = await handler(request)
  File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/site-packages/aiohttp/", line 88, in impl
    return await handler(request)
  File "/Users/explosic4/qb/ot-rpc/warehouse/", line 76, in handler_middleware
    args = await parser.parse(args, request)
  File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/site-packages/webargs/", 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/", 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/", 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/", line 119, in _get_value
    value = yield from function(req, name, argobj)
  File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/site-packages/webargs/", 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/", line 537, in json
    return loads(body)
  File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/json/", line 354, in loads
    return _default_decoder.decode(s)
  File "/Users/explosic4/anaconda3/envs/qb/lib/python3.6/json/", 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/", 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)

This comment has been minimized.

Copy link

commented May 25, 2018

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!


This comment has been minimized.

Copy link

commented Jul 26, 2018

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


This comment has been minimized.

Copy link

commented Sep 25, 2018

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.

@sloria sloria closed this Oct 25, 2018


This comment has been minimized.

Copy link

commented Oct 25, 2018

This is fixed in 4.1.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
4 participants
You can’t perform that action at this time.