Skip to content

Commit

Permalink
docs(recipes): improve the plain text media handler recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
vytas7 committed Jan 1, 2025
1 parent a463a73 commit 2ab64a8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docs/user/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,8 @@ types:
Response Handling
~~~~~~~~~~~~~~~~~

.. _resp_media_data_text:

When would I use media, data, text, and stream?
-----------------------------------------------

Expand Down
25 changes: 17 additions & 8 deletions docs/user/recipes/plain-text-handler.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@
Handling Plain Text as Media
============================

This example demonstrates how to create a custom handler in Falcon to
process the ``text/plain`` media type. The handler implements serialization
and deserialization of textual content, respecting the charset specified
in the ``Content-Type`` header (or defaulting to ``utf-8`` when no charset is provided).
Normally, it is easiest to render a plain text response by setting
:attr:`resp.text <falcon.Response.text>`
(see also: :ref:`resp_media_data_text`).
However, if plain text is just one of
the :ref:`Internet media types <media>` that your application speaks, it may be
useful to generalize handling plain text as :ref:`media <media>` too.

This recipe demonstrates how to create a
:ref:`custom media handler <custom-media-handler-type>` to process the
``text/plain`` media type. The handler implements serialization and
deserialization of textual content, respecting the charset specified in the
``Content-Type`` header
(or defaulting to ``utf-8`` when no charset is provided).

.. literalinclude:: ../../../examples/recipes/plain_text_main.py
:language: python

To use this handler, register it in the Falcon application's media
options for both request and response:
To use this handler, :ref:`register <custom_media_handlers>` it in the Falcon
application's media options for both request and response:

.. code:: python
Expand All @@ -27,5 +36,5 @@ With this setup, the application can handle textual data directly
as ``text/plain``, ensuring support for various character encodings as needed.

.. warning::
Be sure to validate and limit the size of incoming data when
working with textual content to prevent server overload or denial-of-service attacks.
Be sure to validate and limit the size of incoming data when working with
textual content to prevent server overload or denial-of-service attacks.

0 comments on commit 2ab64a8

Please sign in to comment.