How to manipulate the response object in django-piston?
Question: I have some existing python code that uses django-piston which returns a
dictionary as its response. For example:

    
    
    from piston.handler import BaseHandler
    
    class FooHandler(BaseHandler):
        allowed_methods = ('GET',)
    
        @classmethod
        def create(self, request):
            return { 'foo': 'bar' }
    

This code works fine, and is serialized into JSON with the appropriate HTTP
header set (I'm assuming this works by some piston magic involving emitters;
for bonus points, feel free to clarify how this behavior works as well, as I'm
still getting to know django-piston).

I need to be able to modify the response in arbitrary ways, e.g. setting
headers, status codes, etc. without using some pre-baked solution designed for
a specific purpose. Is there a convenient way to access the response object in
the context of this code and manipulate it, or has the response object not yet
been created? In order to get access to a response object, will I have to
construct it manually (a la vanilla django), serialize the dictionary, and set
the appropriate headers by hand, and thus lose out on some of the useful magic
of django-piston?


Answer: Every django-piston method returns an HTTPResponse.

When you return that dictionary, django-piston is just serializing it and
sticking it in an HTTPResponse that it has crafted of some variety.

Kind of surprised you didn't pick up on that given that those "return
rc.CREATED" lines in all the django-piston examples in the wiki are just
hyper-simplistic responses with an HTTP code and response body.

Take a look here: <https://bitbucket.org/jespern/django-
piston/src/c4b2d21db51a/piston/utils.py>

at the rc_factory class, which creates a variety of simple HTTPResponse
objects for use with Piston.

At the very least you can observe how they do it, and then craft your own.

But the answer to the essence of your question "can I make a custom
HTTPResponse" is yes, you can. Of course, that's what web servers _do_.

