Django Piston 内容类型始终为空

发布于 2024-09-13 04:57:41 字数 1262 浏览 2 评论 0 原文

一周前我让 django-piston 工作,但最近我无法调用任何网络服务。下面是一个简单的例子。我有一个“测试”服务,如果存在内容类型,则返回“是”;如果内容类型为空,则返回“否”。我这样做是因为当我执行 POST 并尝试通过“data = request.data”解析我的参数时收到 HTTP 500 错误。我假设我无法执行 request.data 因为内容类型为 null?

所以,这是我的简单 Web 服务:

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

这是 urls.py 文件:

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),
)

最后是我从 python 终端运行来调用该服务的代码:

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

所以“结果”总是返回 no,如果我将“request. data' 在服务中我收到 HTTP 500 错误。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ花落谁相伴 2024-09-20 04:57:41

我认为 data 属性="nofollow noreferrer">HttpRequest 对象。您可能正在寻找 raw_post_data

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文