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

ClientWebSocketResponse with a json method #862

Closed
greut opened this issue May 5, 2016 · 8 comments · Fixed by #897
Closed

ClientWebSocketResponse with a json method #862

greut opened this issue May 5, 2016 · 8 comments · Fixed by #897
Labels

Comments

@greut
Copy link

greut commented May 5, 2016

Long story short

I would love to have a json() method next to the data property of a ClientWebSocketResponse.

Expected behaviour

That I can do msg.json() instead of manually json.loads(msg.data) just like the HTTP response.

Actual behaviour

No json()

Steps to reproduce

async with aiohttp.ws_connect(url) as ws:
    while True:
        msg = await ws.receive()

        if msg.tp == aiohttp.MsgType.text:
            print(json.loads(msg.data))
            print(msg.json())
        else:
            print(msg.tp, file=sys.stderr)
            break

Your environment

Python 3.5, Archlinux, GNU/Linux 4.5.1 x86_64

@asvetlov
Copy link
Member

asvetlov commented May 5, 2016

Should msg.data be stripped?

@greut
Copy link
Author

greut commented May 6, 2016

I don't understand the stripped idea?

Maybe a better way is to deal with it like the receive_str and receive_bytes, adding a receive_json:

master...greut:862-json

@asvetlov
Copy link
Member

I mean should receive_json() work like return json.loads(await self.receive_str()) or return json.loads(await self.receive_str().strip()).

And what to do with empty string data?

@greut
Copy link
Author

greut commented May 29, 2016

After some use of it, it's hard to beat for msg in ws: simplicity. It's probably not adding much value vs complexity.

@asvetlov
Copy link
Member

asvetlov commented Jun 3, 2016

Fixed by #897

@greut
Copy link
Author

greut commented Jun 3, 2016

@asvetlov Great! Thanks mate.

@asvetlov
Copy link
Member

asvetlov commented Jun 3, 2016

Thanks to @sseg

@lock
Copy link

lock bot commented Oct 29, 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 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants