mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Docs + unit tests for Response, closes #821
This commit is contained in:
parent
f5e79adf26
commit
db660db463
4 changed files with 86 additions and 1 deletions
|
|
@ -80,6 +80,54 @@ Consider the querystring ``?foo=1&foo=2&bar=3`` - with two values for ``foo`` an
|
|||
``len(request.args)`` - integer
|
||||
Returns the number of keys.
|
||||
|
||||
.. _internals_response:
|
||||
|
||||
Response class
|
||||
~~~~~~~~~~~~~~
|
||||
|
||||
The ``Response`` class can be returned from view functions that have been registered using the :ref:`plugin_register_routes` hook.
|
||||
|
||||
The ``Response()`` constructor takes the following arguments:
|
||||
|
||||
``body`` - string
|
||||
The body of the response.
|
||||
|
||||
``status`` - integer (optional)
|
||||
The HTTP status - defaults to 200.
|
||||
|
||||
``headers`` - dictionary (optional)
|
||||
A dictionary of extra HTTP headers, e.g. ``{"x-hello": "world"}``.
|
||||
|
||||
``content_type`` - string (optional)
|
||||
The content-type for the response. Defaults to ``text/plain``.
|
||||
|
||||
For example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from datasette.utils.asgi import Response
|
||||
|
||||
response = Response(
|
||||
"<xml>This is XML</xml>",
|
||||
content_type="application/xml; charset=utf-8"
|
||||
)
|
||||
|
||||
The easiest way to create responses is using the ``Response.text(...)``, ``Response.html(...)``, ``Response.json(...)`` or ``Response.redirect(...)`` helper methods:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from datasette.utils.asgi import Response
|
||||
|
||||
html_response = Response.html("This is HTML")
|
||||
json_response = Response.json({"this_is": "json"})
|
||||
text_response = Response.text("This will become utf-8 encoded text")
|
||||
# Redirects are served as 302, unless you pass status=301:
|
||||
redirect_response = Response.redirect("https://latest.datasette.io/")
|
||||
|
||||
Each of these responses will use the correct corresponding content-type - ``text/html; charset=utf-8``, ``application/json; charset=utf-8`` or ``text/plain; charset=utf-8`` respectively.
|
||||
|
||||
Each of the helper methods take optional ``status=`` and ``headers=`` arguments, documented above.
|
||||
|
||||
.. _internals_datasette:
|
||||
|
||||
Datasette class
|
||||
|
|
|
|||
|
|
@ -882,7 +882,7 @@ The optional view function arguments are as follows:
|
|||
``receive`` - function
|
||||
The ASGI receive function.
|
||||
|
||||
The function can either return a ``Response`` or it can return nothing and instead respond directly to the request using the ASGI ``receive`` function (for advanced uses only).
|
||||
The function can either return a :ref:`internals_response` or it can return nothing and instead respond directly to the request using the ASGI ``send`` function (for advanced uses only).
|
||||
|
||||
.. _plugin_register_facet_classes:
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue