falconry / falcon

The no-magic web data plane API and microservices framework for Python developers, with a focus on reliability, correctness, and performance at scale.
https://falcon.readthedocs.io/en/stable/
Apache License 2.0
9.52k stars 944 forks source link

Media handler for `text/plain` #2037

Open vytas7 opened 2 years ago

vytas7 commented 2 years ago

As proposed by @maxking on Gitter:

I have been trying to add support for msgpack in Mailman's API (mailman/mailman!977) and in the process of doing that, I am trying to move off of doing the serialization in our code and setting the request.body/text to setting the response.media (in the actual resource) and response.content_type (before hand, in a middleware using request.accept) to let Falcon do the right thing. One thing I started noticing is that we were still returning some plain text and there is no default handler for text/plain response (presumably because it can be done with response.text or something). I was wondering if y'all would be open to adding a handler for text/plain, which basically works like identity?

vytas7 commented 2 years ago

I've sketched a prototype how this could look like: https://gist.github.com/vytas7/7224c6d552c6e01992dfd84c01a40063.

Most probably, we won't be willing to enable such a handler by default in the first incarnation (because that would be a breaking change for req handlers; debatable for resp handlers). We could either add an optional handler that is not installed by default (like MessagePackHandler), or morph my gist into a docs recipe.

maxking commented 2 years ago

While both Docs and a handler not enabled by default would do the job, if there is any interest in supporting the text/plain handler in the long term, perhaps adding it as handler would enable more usage for it.

Although, if we aren't sure about how the handler should work for different charsets or its design (i.e. can be breaking change over next few releases) then we can add it as a docs recipe until we finalize the design of it.

vytas7 commented 1 month ago

Let's start with a recipe.

Note that recipes now need to be covered with unit tests as well.