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

Closed
explosic4 opened this issue May 25, 2018 · 4 comments
Closed

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

explosic4 opened this issue May 25, 2018 · 4 comments

Comments

@explosic4
Copy link

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)
@sloria
Copy link
Member

sloria 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!

@foresmac
Copy link

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

@fmqa
Copy link
Contributor

fmqa 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 as completed Oct 25, 2018
@sloria
Copy link
Member

sloria 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
Projects
None yet
Development

No branches or pull requests

4 participants