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

Add API to easily send JSON as request body #1726

Closed
socketpair opened this issue Mar 18, 2017 · 9 comments
Closed

Add API to easily send JSON as request body #1726

socketpair opened this issue Mar 18, 2017 · 9 comments
Labels

Comments

@socketpair
Copy link
Contributor

socketpair commented Mar 18, 2017

i.e. some shortcut for

data = {'answer': 42}
client_session.post(
    'http://example.com',
    data=json.dumps(answer),
    headers={'content-type': 'application/json'}
)
@socketpair
Copy link
Contributor Author

socketpair commented Mar 18, 2017

Tornado automatically makes json.dumps + set content-type when it see that isinstance(data, dict). This covers many usecases. But even with this, we needed to patch tornado. We send lists as json. So, as I think, most intuitive API will be:

  • post(json=..). -- calls json.dumps() and sets content-type application/json (if content-type was not set using headers=...
  • post(text=..). -- calls .encode('utf-8') and sets content-type text/plain; charset=utf-8 (if content-type was not set using headers=...

This will match how the web.Response() works. But unfortunatelly only for data=... and text=.... Also, it will be nice to add json=... to web.Response, and say that web.json_response is obsolete.

@fafhrd91
Copy link
Member

we can add payload for dict and list, something like https://github.com/aio-libs/aiohttp/blob/master/aiohttp/payload.py#L199

I'd add "json=" and "text=" but we should be very careful, request() method has so many parameters already.

@AraHaan
Copy link
Contributor

AraHaan commented Mar 18, 2017

discord.py can request json like so (They basically made a class that creates their own request function that has an param named json

import aiohttp
import asyncio
import json

class HTTPClient:
    def __init__(self, loop, *args, *kwargs):
        self.loop = loop
        self.session = aiohttp.ClientSession(loop=self.loop, *args, **kwargs)

    @asyncio.coroutine
    def request(self, url, method, **kwargs):
        if 'json' in kwargs:
            headers['Content-Type'] = 'application/json'
            kwargs['headers'] = headers
            kwargs['data'] = json.dumps(kwargs.pop('json'), separators=(',', ':'), ensure_ascii=True)
        r = yield from self.session.request(method, url, **kwargs)
        yield from r.release()

As you can see json can be passed just fine into data=.

@fafhrd91
Copy link
Member

added json parameter, but I am not sure about text

@fafhrd91
Copy link
Member

regarding json for web.Response(), @asvetlov has some reasons not to add json.
@asvetlov could you clarify?

@socketpair
Copy link
Contributor Author

Great! I have now more reasons to throw out Tornado from our projects!

@jettify
Copy link
Member

jettify commented Mar 20, 2017

previous discussion about json inweb.Response() #592

@fafhrd91
Copy link
Member

added separate ticket for web.Response

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants