Skip to content

Commit

Permalink
doc(Tutorial): Improvements to the msgpack example
Browse files Browse the repository at this point in the history
Add more detailed instructions on how to use msgpack as a serialization
alternative to the default JSON content_type, including installation of
required packages from PyPI.

Fixes #522
  • Loading branch information
sebasmagri authored and Kurt Griffiths committed May 8, 2015
1 parent 1644ca5 commit 8bd55ce
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions doc/user/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,28 @@ params, as we shall see later on.

Right now, the image resource responds to GET requests with a simple
``200 OK`` and a JSON body. Falcon's Internet media type defaults to
``application/json`` but you can set it to whatever you like. See
serialization with `MessagePack <http://msgpack.org/>`_ for example:
``application/json`` but you can set it to whatever you like. For example,
you could use `MessagePack <http://msgpack.org/>`_, or any other
serialization format.

If you'd like to use MessagePack in the above example, you'll need to
install the (de)serializer for Python running ``pip install msgpack-python``
and then update your responder to set the response data and content_type
accordingly:

.. code:: python
def on_get(self, req, resp):
resp.data = msgpack.packb({'message': 'Hello world!'})
resp.content_type = 'application/msgpack'
resp.status = falcon.HTTP_200
import falcon
import msgpack
class Resource(object):
def on_get(self, req, resp):
resp.data = msgpack.packb({'message': 'Hello world!'})
resp.content_type = 'application/msgpack'
resp.status = falcon.HTTP_200
Note the use of ``resp.data`` in lieu of ``resp.body``. If you assign a
bytestring to the latter, Falcon will figure it out, but you can
Expand Down

0 comments on commit 8bd55ce

Please sign in to comment.