Search code examples
pythondjangodjango-piston

Django Piston Content Type Always Null


I had django-piston working a week ago but recently I'm unable to call any web services. Below is a simple example. I have a 'test' service that returns 'yes' if there is a content type and 'no' if content type is null. I've done this because I get HTTP 500 errors when I do a POST and try to parse my parameters via 'data = request.data'. I'm assuming I can't do request.data because the content type is null?

So, here is my simple web service:

class testHandler(BaseHandler):
     def create(self, request):
         if request.content_type:
              return 'yes'
         else:
              data = request.data
              return 'no'

And here is the urls.py file:

class CsrfExemptResource( Resource ):
     def __init__( self, handler, authentication = None ):
         super( CsrfExemptResource, self ).__init__( handler, authentication )
         self.csrf_exempt = getattr( self.handler, 'csrf_exempt', True )

controller_handler = CsrfExemptResource(controllerHandler)
test_handler = CsrfExemptResource(testHandler)

urlpatterns = patterns('',
     url(r'^controller/', controller_handler),
     url(r'^test/', test_handler),
)

And finally the code I run from my python terminal to call the service:

params = urllib.urlencode({'value':'someValue'}) 
req = urllib2.Request("http://127.0.0.1/cindy/api/test/", params) 
result = urllib2.urlopen(req).read() 

So 'result' always return no, and if I put the line 'request.data' in the service I get a HTTP 500 error.

Thanks in advance.


Solution

  • I don't think there is a data attribute in the HttpRequest object. You might be looking for raw_post_data.