Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upCustom media handlers: Unexpected issue when providing custom json handler #1717
Comments
|
Hi Thanks for using Falcon. The large amount of time and effort needed to Please consider helping us secure the future of the Falcon framework with a Thank you for your support! |
|
I agree we should make this better since it violates the principle of least-surprise. Scheduled for 3.0 in case someone wants to tackle it right away, otherwise it will need to be pushed to 3.1. |
|
FWIW, our media type negotiation machinery seems to automatically choose the As a bonus, that would eliminate confusion in situations like @adsahay is describing. I was testing the app below, and it seems to just work: import falcon
class Resource:
def on_post(self, req, resp):
resp.media = req.get_media()
app = falcon.App()
app.req_options.media_handlers = falcon.media.Handlers({
'application/json': falcon.media.JSONHandler(),
})
app.add_route('/mirror', Resource())Thoughts? |
|
Yes this sounds great. Also, it might be a stylistic preference, but in such situations if we can have a constant for all handlers (falcon.MIME_JSON) or something it will have the additional benefit of discoverability of available handlers in documentation / code completion, and avoid typos. |
|
If you want I can fix and send PR for this (my first falcon contribution!) |
|
Hi again @adsahay , >>> import falcon
>>> falcon.MEDIA_JSON
'application/json'
>>> falcon.MEDIA_PNG
'image/png'
>>> falcon.MEDIA_TEXT
'text/plain; charset=utf-8'@adsahay PRs are always welcome @kgriffs thoughts on my suggestion to simplify the default JSON handlers by only keeping the |
|
It could be that the key including the charset was left in to speed up the selection algorithm... regardless, I agree that it is not worth maintaining it since in most cases the charset will not be present in the string to begin with. |
|
@adsahay just checking, would you like to work on this by removing As we proceed to remove it though, I would like to see a note added to the docs regarding speeding up the selection algorithm (needs to be verified with a benchmark!), as hinted by @kgriffs above. So that people could duplicate their handlers for a specific parameter set, if they expect that to be their common use case that needs to be optimized. |
|
Ok sure. |


This is in falcon-2.0
Look at the documentation here for using rapidjson for encoding/decoding json. By providing:
extra_handlers={'application/json': json_handler}we are still left with the default handler for content-typeapplication-json; charset=UTF-8. This results in an unexpected behaviour when some client library (e.g. Retrofit for Android) includes the charset in the header.While the documentation should be updated, the expected behaviour is that if the handler for
application/jsonis updated - it should also update the handler for variant with charset (or at least throw a warning) otherwise there is a possibility of hidden bugs.