pythondjangoresthttpdjango-piston

How do I use Django piston to return a response in text/plain?


I want to NOT serialize anything. I just want to return what is equivalent to HttpResponse(blah)


Solution

  • Sounds like you want a string emitter, and not one of the built-in JSONEmitter, XMLEmitter, etc.

    Have a look at the docs for emitters: https://bitbucket.org/jespern/django-piston/wiki/Documentation

    And the existing emitter definitions here: https://bitbucket.org/jespern/django-piston/src/c4b2d21db51a/piston/emitters.py

    A definition of a plain text emitter might look like this:

    from piston.emitters import Emitter
    from piston.utils import Mimer    
    
    class TextEmitter(Emitter):
        def render(self, request):
            return self.construct()
    Emitter.register('text', TextEmitter)
    Mimer.register('text', None, ('text/plain',))
    

    You'd get your resource to use this emitter in your urls.py like so:

    urlpatterns = patterns('',
       url(r'^blogposts$', resource_here, { 'emitter_format': 'text' }),
    )