-
-
Notifications
You must be signed in to change notification settings - Fork 947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(recipes): add text/plain media handler recipe #2419
Open
EricGoulart
wants to merge
5
commits into
falconry:master
Choose a base branch
from
diegomirandap:2037
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bb93a41
feat(recipes): add text/plain media handler example
EricGoulart 3a598ae
adding the rst file
EricGoulart 08ae4c4
fix plain/text handler
diegomirandap 4f50bcc
Merge branch 'master' into 2037
diegomirandap ca349ed
reformatted
EricGoulart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ Recipes | |
header-name-case | ||
multipart-mixed | ||
output-csv | ||
plain-text-handler | ||
pretty-json | ||
raw-url-path | ||
request-id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
.. _plain-text-recipe: | ||
|
||
Handling Plain Text | ||
=================== | ||
|
||
.. _custom-text-handler: | ||
|
||
Custom Text Media Handler | ||
========================= | ||
|
||
This example demonstrates how to create a custom handler in Falcon to | ||
process `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 requests and responses: | ||
|
||
.. code:: python | ||
|
||
import falcon | ||
from your_module import TextHandler | ||
|
||
app = falcon.App() | ||
app.req_options.media_handlers['text/plain'] = TextHandler() | ||
app.resp_options.media_handlers['text/plain'] = TextHandler() | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import functools | ||
|
||
import falcon | ||
|
||
|
||
class TextHandler(falcon.media.BaseHandler): | ||
DEFAULT_CHARSET = 'utf-8' | ||
|
||
@classmethod | ||
@functools.lru_cache | ||
def _get_charset(cls, content_type): | ||
_, params = falcon.parse_header(content_type) | ||
return params.get('charset') or cls.DEFAULT_CHARSET | ||
|
||
def deserialize(self, stream, content_type, content_length): | ||
data = stream.read() | ||
return data.decode(self._get_charset(content_type)) | ||
|
||
def serialize(self, media, content_type): | ||
return media.encode(self._get_charset(content_type)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Newline missing at the end of file.