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

Comments

@explosic4
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/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

This comment has been minimized.

Copy link
Member

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

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

@kochab

This comment has been minimized.

Copy link
Contributor

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

@sloria

This comment has been minimized.

Copy link
Member

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
4 participants
You can’t perform that action at this time.