2

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

4 Answers4

2

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' }),
)
Streeter
  • 556
  • 4
  • 22
user85461
  • 6,510
  • 2
  • 34
  • 40
1

Simplest way I found, from the Django docs:

HttpResponse("Text only, please.", content_type="text/plain")
Aidan Feldman
  • 5,205
  • 36
  • 46
0

in your view:

class HttpResponsePlain(django.http.HttpResponse):

    def serialize(self):            return self.content
    def serialize_headers(self):    return ''

return HttpResponsePlain(content = 'That is plain text response!')
0

To add on to user85461's answer, when you make a text emitter, you'll want to also make a text Mimer. I wrote the following code with works with Piston 0.2.2

from piston.emitters import Emitter
from piston.utils import Mimer

class TextEmitter(Emitter):
    def render(self, request):
        return self.construct()
Emitter.register('text', TextEmitter, ('text/plain',))
Mimer.register(lambda x: QueryDict(x), ('text/plain',))

Add this snippet somewhere that will get run before your handlers. I put it in my API urls.py above where I created my Resources with

resource_handler = Resource(handler=SomeHandler)
Streeter
  • 556
  • 4
  • 22