mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
await request.post_body() method, closes #897
This commit is contained in:
parent
c5f06bc356
commit
1f6a134369
3 changed files with 37 additions and 3 deletions
|
|
@ -90,8 +90,7 @@ class Request:
|
||||||
def actor(self):
|
def actor(self):
|
||||||
return self.scope.get("actor", None)
|
return self.scope.get("actor", None)
|
||||||
|
|
||||||
async def post_vars(self):
|
async def post_body(self):
|
||||||
body = []
|
|
||||||
body = b""
|
body = b""
|
||||||
more_body = True
|
more_body = True
|
||||||
while more_body:
|
while more_body:
|
||||||
|
|
@ -99,7 +98,10 @@ class Request:
|
||||||
assert message["type"] == "http.request", message
|
assert message["type"] == "http.request", message
|
||||||
body += message.get("body", b"")
|
body += message.get("body", b"")
|
||||||
more_body = message.get("more_body", False)
|
more_body = message.get("more_body", False)
|
||||||
|
return body
|
||||||
|
|
||||||
|
async def post_vars(self):
|
||||||
|
body = await self.post_body()
|
||||||
return dict(parse_qsl(body.decode("utf-8"), keep_blank_values=True))
|
return dict(parse_qsl(body.decode("utf-8"), keep_blank_values=True))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
|
||||||
|
|
@ -48,11 +48,14 @@ The request object is passed to various plugin hooks. It represents an incoming
|
||||||
``.actor`` - dictionary (str -> Any) or None
|
``.actor`` - dictionary (str -> Any) or None
|
||||||
The currently authenticated actor (see :ref:`actors <authentication_actor>`), or ``None`` if the request is unauthenticated.
|
The currently authenticated actor (see :ref:`actors <authentication_actor>`), or ``None`` if the request is unauthenticated.
|
||||||
|
|
||||||
The object also has one awaitable method:
|
The object also has two awaitable methods:
|
||||||
|
|
||||||
``await request.post_vars()`` - dictionary
|
``await request.post_vars()`` - dictionary
|
||||||
Returns a dictionary of form variables that were submitted in the request body via ``POST``. Don't forget to read about :ref:`internals_csrf`!
|
Returns a dictionary of form variables that were submitted in the request body via ``POST``. Don't forget to read about :ref:`internals_csrf`!
|
||||||
|
|
||||||
|
``await request.post_body()`` - bytes
|
||||||
|
Returns the un-parsed body of a request submitted by ``POST`` - useful for things like incoming JSON data.
|
||||||
|
|
||||||
.. _internals_multiparams:
|
.. _internals_multiparams:
|
||||||
|
|
||||||
The MultiParams class
|
The MultiParams class
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
from datasette.utils.asgi import Request
|
from datasette.utils.asgi import Request
|
||||||
|
import json
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -26,6 +27,34 @@ async def test_request_post_vars():
|
||||||
assert {"foo": "bar", "baz": "1", "empty": ""} == await request.post_vars()
|
assert {"foo": "bar", "baz": "1", "empty": ""} == await request.post_vars()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_request_post_body():
|
||||||
|
scope = {
|
||||||
|
"http_version": "1.1",
|
||||||
|
"method": "POST",
|
||||||
|
"path": "/",
|
||||||
|
"raw_path": b"/",
|
||||||
|
"query_string": b"",
|
||||||
|
"scheme": "http",
|
||||||
|
"type": "http",
|
||||||
|
"headers": [[b"content-type", b"application/json"]],
|
||||||
|
}
|
||||||
|
|
||||||
|
data = {"hello": "world"}
|
||||||
|
|
||||||
|
async def receive():
|
||||||
|
return {
|
||||||
|
"type": "http.request",
|
||||||
|
"body": json.dumps(data, indent=4).encode("utf-8"),
|
||||||
|
"more_body": False,
|
||||||
|
}
|
||||||
|
|
||||||
|
request = Request(scope, receive)
|
||||||
|
body = await request.post_body()
|
||||||
|
assert isinstance(body, bytes)
|
||||||
|
assert data == json.loads(body)
|
||||||
|
|
||||||
|
|
||||||
def test_request_args():
|
def test_request_args():
|
||||||
request = Request.fake("/foo?multi=1&multi=2&single=3")
|
request = Request.fake("/foo?multi=1&multi=2&single=3")
|
||||||
assert "1" == request.args.get("multi")
|
assert "1" == request.args.get("multi")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue