Media¶
Falcon allows for easy and customizable internet media type handling. By default
Falcon only enables a single JSON handler. However, additional handlers
can be configured through the falcon.RequestOptions
and
falcon.ResponseOptions
objects specified on your falcon.API
.
Note
To avoid unnecessary overhead, Falcon will only process request media the first time the media property is referenced. Once it has been referenced, it’ll use the cached result for subsequent interactions.
Usage¶
Zero configuration is needed if you’re creating a JSON API. Just access
or set the media
attribute as appropriate and let Falcon do the heavy
lifting for you.
import falcon
class EchoResource(object):
def on_post(self, req, resp):
message = req.media.get('message')
resp.media = {'message': message}
resp.status = falcon.HTTP_200
Warning
Once media is called on a request, it’ll consume the request’s stream.
Validating Media¶
Falcon currently only provides a JSON Schema media validator; however, JSON Schema is very versatile and can be used to validate any deserialized media type that JSON also supports (i.e. dicts, lists, etc).
-
falcon.media.validators.jsonschema.
validate
(req_schema=None, resp_schema=None)[source]¶ Decorator for validating
req.media
using JSON Schema.This decorator provides standard JSON Schema validation via the
jsonschema
package available from PyPI. Semantic validation via the format keyword is enabled for the default checkers implemented byjsonschema.FormatChecker
.Note
The jsonschema` package must be installed separately in order to use this decorator, as Falcon does not install it by default.
See json-schema.org for more information on defining a compatible dictionary.
- Parameters
req_schema (dict, optional) – A dictionary that follows the JSON Schema specification. The request will be validated against this schema.
resp_schema (dict, optional) – A dictionary that follows the JSON Schema specification. The response will be validated against this schema.
Example
from falcon.media.validators import jsonschema # -- snip -- @jsonschema.validate(my_post_schema) def on_post(self, req, resp): # -- snip --
If JSON Schema does not meet your needs, a custom validator may be implemented in a similar manner to the one above.
Content-Type Negotiation¶
Falcon currently only supports partial negotiation out of the box. By default,
when the media
attribute is used it attempts to de/serialize based on the
Content-Type
header value. The missing link that Falcon doesn’t provide
is the connection between the falcon.Request
Accept
header provided
by a user and the falcon.Response
Content-Type
header.
If you do need full negotiation, it is very easy to bridge the gap using middleware. Here is an example of how this can be done:
class NegotiationMiddleware(object):
def process_request(self, req, resp):
resp.content_type = req.accept
Replacing the Default Handlers¶
When creating your API object you can either add or completely
replace all of the handlers. For example, lets say you want to write an API
that sends and receives MessagePack. We can easily do this by telling our
Falcon API that we want a default media-type of application/msgpack
and
then create a new Handlers
object specifying the desired media type and
a handler that can process that data.
import falcon
from falcon import media
handlers = media.Handlers({
'application/msgpack': media.MessagePackHandler(),
})
api = falcon.API(media_type='application/msgpack')
api.req_options.media_handlers = handlers
api.resp_options.media_handlers = handlers
Alternatively, if you would like to add an additional handler such as MessagePack, this can be easily done in the following manner:
import falcon
from falcon import media
extra_handlers = {
'application/msgpack': media.MessagePackHandler(),
}
api = falcon.API()
api.req_options.media_handlers.update(extra_handlers)
api.resp_options.media_handlers.update(extra_handlers)
Supported Handler Types¶
-
class
falcon.media.
JSONHandler
(dumps=None, loads=None)[source]¶ JSON media handler.
This handler uses Python’s standard
json
library by default, but can be easily configured to use any of a number of third-party JSON libraries, depending on your needs. For example, you can often realize a significant performance boost under CPython by using an alternative library. Good options in this respect include orjson, python-rapidjson, and mujson.Note
If you are deploying to PyPy, we recommend sticking with the standard library’s JSON implementation, since it will be faster in most cases as compared to a third-party library.
Overriding the default JSON implementation is simply a matter of specifying the desired
dumps
andloads
functions:import falcon from falcon import media import rapidjson json_handler = media.JSONHandler( dumps=rapidjson.dumps, loads=rapidjson.loads, ) extra_handlers = { 'application/json': json_handler, } api = falcon.API() api.req_options.media_handlers.update(extra_handlers) api.resp_options.media_handlers.update(extra_handlers)
By default,
ensure_ascii
is passed to thejson.dumps
function. If you override thedumps
function, you will need to explicitly setensure_ascii
toFalse
in order to enable the serialization of Unicode characters to UTF-8. This is easily done by usingfunctools.partial
to apply the desired keyword argument. In fact, you can use this same technique to customize any option supported by thedumps
andloads
functions:from functools import partial from falcon import media import rapidjson json_handler = media.JSONHandler( dumps=partial( rapidjson.dumps, ensure_ascii=False, sort_keys=True ), )
- Keyword Arguments
dumps (func) – Function to use when serializing JSON responses.
loads (func) – Function to use when deserializing JSON requests.
-
deserialize
(stream, content_type, content_length)[source]¶ Deserialize the
falcon.Request
body.- Parameters
stream (object) – Input data to deserialize.
content_type (str) – Type of request content.
content_length (int) – Length of request content.
- Returns
A deserialized object.
- Return type
object
-
serialize
(media, content_type)[source]¶ Serialize the media object on a
falcon.Response
- Parameters
media (object) – A serializable object.
content_type (str) – Type of response content.
- Returns
The resulting serialized bytes from the input object.
- Return type
bytes
-
class
falcon.media.
MessagePackHandler
[source]¶ Handler built using the
msgpack
module.This handler uses
msgpack.unpackb()
andmsgpack.packb()
. The MessagePackbin
type is used to distinguish between Unicode strings (str
on Python 3,unicode
on Python 2) and byte strings (bytes
on Python 2/3, orstr
on Python 2).Note
This handler requires the extra
msgpack
package (version 0.5.2 or higher), which must be installed in addition tofalcon
from PyPI:$ pip install msgpack
-
deserialize
(stream, content_type, content_length)[source]¶ Deserialize the
falcon.Request
body.- Parameters
stream (object) – Input data to deserialize.
content_type (str) – Type of request content.
content_length (int) – Length of request content.
- Returns
A deserialized object.
- Return type
object
-
serialize
(media, content_type)[source]¶ Serialize the media object on a
falcon.Response
- Parameters
media (object) – A serializable object.
content_type (str) – Type of response content.
- Returns
The resulting serialized bytes from the input object.
- Return type
bytes
-
Custom Handler Type¶
If Falcon doesn’t have an internet media type handler that supports your use case, you can easily implement your own using the abstract base class provided by Falcon:
-
class
falcon.media.
BaseHandler
[source]¶ Abstract Base Class for an internet media type handler
-
abstract
serialize
(media, content_type)[source]¶ Serialize the media object on a
falcon.Response
- Parameters
media (object) – A serializable object.
content_type (str) – Type of response content.
- Returns
The resulting serialized bytes from the input object.
- Return type
bytes
-
abstract
deserialize
(stream, content_type, content_length)[source]¶ Deserialize the
falcon.Request
body.- Parameters
stream (object) – Input data to deserialize.
content_type (str) – Type of request content.
content_length (int) – Length of request content.
- Returns
A deserialized object.
- Return type
object
-
abstract
Handlers¶
Media Type Constants¶
The falcon
module provides a number of constants for
common media types, including the following:
falcon.MEDIA_JSON
falcon.MEDIA_MSGPACK
falcon.MEDIA_YAML
falcon.MEDIA_XML
falcon.MEDIA_HTML
falcon.MEDIA_JS
falcon.MEDIA_TEXT
falcon.MEDIA_JPEG
falcon.MEDIA_PNG
falcon.MEDIA_GIF