.. _internals: ======================= Internals for plugins ======================= Many :ref:`plugin_hooks` are passed objects that provide access to internal Datasette functionality. The interface to these objects should not be considered stable with the exception of methods that are documented here. .. _internals_request: Request object ============== The request object is passed to various plugin hooks. It represents an incoming HTTP request. It has the following properties: ``.scope`` - dictionary The ASGI scope that was used to construct this request, described in the `ASGI HTTP connection scope `__ specification. ``.method`` - string The HTTP method for this request, usually ``GET`` or ``POST``. ``.url`` - string The full URL for this request, e.g. ``https://latest.datasette.io/fixtures``. ``.scheme`` - string The request scheme - usually ``https`` or ``http``. ``.headers`` - dictionary (str -> str) A dictionary of incoming HTTP request headers. Header names have been converted to lowercase. ``.cookies`` - dictionary (str -> str) A dictionary of incoming cookies ``.host`` - string The host header from the incoming request, e.g. ``latest.datasette.io`` or ``localhost``. ``.path`` - string The path of the request excluding the query string, e.g. ``/fixtures``. ``.full_path`` - string The path of the request including the query string if one is present, e.g. ``/fixtures?sql=select+sqlite_version()``. ``.query_string`` - string The query string component of the request, without the ``?`` - e.g. ``name__contains=sam&age__gt=10``. ``.args`` - MultiParams An object representing the parsed query string parameters, see below. ``.url_vars`` - dictionary (str -> str) Variables extracted from the URL path, if that path was defined using a regular expression. See :ref:`plugin_register_routes`. ``.actor`` - dictionary (str -> Any) or None The currently authenticated actor (see :ref:`actors `), or ``None`` if the request is unauthenticated. The object also has the following awaitable methods: ``await request.form(files=False, ...)`` - FormData Parses form data from the request body. Supports both ``application/x-www-form-urlencoded`` and ``multipart/form-data`` content types. Returns a :ref:`internals_formdata` object with dict-like access to form fields and uploaded files. Requirements and errors: - A ``Content-Type`` header is required. Missing or unsupported content types raise ``BadRequest``. - For ``multipart/form-data``, the ``boundary=...`` parameter is required. Parameters: - ``files`` (bool, default ``False``): If ``True``, uploaded files are stored and accessible. If ``False`` (default), file content is discarded but form fields are still available. - ``max_file_size`` (int, default 50MB): Maximum size per uploaded file in bytes. - ``max_request_size`` (int, default 100MB): Maximum total request body size in bytes. - ``max_fields`` (int, default 1000): Maximum number of form fields. - ``max_files`` (int, default 100): Maximum number of uploaded files. - ``max_parts`` (int, default ``max_fields + max_files``): Maximum number of multipart parts in total. - ``max_field_size`` (int, default 100KB): Maximum size of a text field value in bytes. - ``max_memory_file_size`` (int, default 1MB): File size threshold before uploads spill to disk. - ``max_part_header_bytes`` (int, default 16KB): Maximum total bytes allowed in part headers. - ``max_part_header_lines`` (int, default 100): Maximum header lines per part. - ``min_free_disk_bytes`` (int, default 50MB): Minimum free bytes required in the temp directory before accepting file uploads. Example usage: .. code-block:: python # Parse form fields only (files are discarded) form = await request.form() username = form["username"] tags = form.getlist("tags") # For multiple values # Parse form fields AND files form = await request.form(files=True) uploaded = form["avatar"] content = await uploaded.read() print( uploaded.filename, uploaded.content_type, uploaded.size ) Cleanup note: When using ``files=True``, call ``await form.aclose()`` once you are done with the uploads to ensure spooled temporary files are closed promptly. You can also use ``async with form: ...`` for automatic cleanup. Don't forget to read about :ref:`internals_csrf`! ``await request.post_vars()`` - MultiParams Returns a :ref:`MultiParams ` object of form variables that were submitted in the request body via ``POST`` using ``application/x-www-form-urlencoded`` encoding. This has the same shape as ``request.args``, so use ``.getlist(key)`` to retrieve every value submitted for keys with multiple values (such as ``