I need to access the query parameter dict from Django Channels.
The url may look like this: ws://127.0.0.1:8000/?hello="world"
How do I retrieve 'world' like this: query_params["hello"]?
I need to access the query parameter dict from Django Channels.
The url may look like this: ws://127.0.0.1:8000/?hello="world"
How do I retrieve 'world' like this: query_params["hello"]?
Updated for Channel 3:
from urllib.parse import parse_qs
# Types
class Scope(TypedDict):
query_string: bytes
scope: Scope
query_params: Dict[str, List[str]]
# Parse query_string
query_params = parse_qs(scope["query_string"].decode())
print(query_params["access_token"][-1])
If you were doing this a lot, you could put it in a middleware and wrap your ASGI application in it. Something like:
class QueryParamsMiddleware(BaseMiddleware):
async def __call__(self, scope, receive, send):
scope = dict(scope)
scope["query_params"] = parse_qs(scope["query_string"].decode())
return await super().__call__(scope, receive, send)