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

JSONDecodeError when using default locations and required=True argument #80

Closed
leonidumanskiy opened this issue Nov 12, 2015 · 3 comments

Comments

@leonidumanskiy
Copy link

commented Nov 12, 2015

When using default setup with aiohttp and default locations, the server will throw exception "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)" if the client doesn't send the required parameter and there is no json data in the http request.

I am not sure if that's a bug or expected behavior, but that's absolutely counter-intuitive, especially if you just started to use the library.

@sloria

This comment has been minimized.

Copy link
Member

commented Nov 14, 2015

@leonidumanskiy I am not able to reproduce this bug. Can you please post code that is raising the error?

@leonidumanskiy

This comment has been minimized.

Copy link
Author

commented Nov 14, 2015

@sloria

import asyncio
from aiohttp import web
from webargs import fields
from webargs.aiohttpparser import parser

handler_args = {
    'name': fields.Str(required=True)
}

async def handler(request):
    args = await parser.parse(handler_args, request)
    return web.Response(
        body='Hello, {}'.format(args['name']).encode('utf-8')
    )


async def init(loop):
    app = web.Application(loop=loop)
    app.router.add_route('POST', '/', handler)

    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 8080)
    print("Server started at http://127.0.0.1:8080")
    return srv

try:
    loop = asyncio.get_event_loop()
    loop.run_until_complete(init(loop))
    loop.run_forever()
except KeyboardInterrupt:
    pass
curl http://127.0.0.1:8080/ -X POST --data "aaa=123"

See how I put absolutely random stuff to the post data. However, if you don't post any data in the query at all, the bug doesn't seem to work. Sorry I forgot to mention this detail in the initial post.

@sloria

This comment has been minimized.

Copy link
Member

commented Nov 14, 2015

Thanks for the clarification. I'll look into a fix for this today.

@sloria sloria added the bug label Nov 14, 2015

@sloria sloria closed this in ba880d8 Nov 14, 2015

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