向 tastypie 发送经过身份验证的 POST 请求
我正在尝试将 ApiKey 验证的 POST 请求发送到 tastypie API
我的模型:
class Thing(models.Model):
name = models.TextField()
def __unicode__(self):
return u'%s'%self.name
我的 ModelResource
class ThingResource(ModelResource):
class Meta:
queryset = Thing.objects.all()
resource_name="thing"
authentication = ApiKeyAuthentication()
authorization = DjangoAuthorization()
我的 urls.py
from django.conf.urls.defaults import patterns, include, url
from tastypie.api import Api
from myapp.api import ThingResource
mobile_api = Api(api_name='mobile')
mobile_api.register(ThingResource())
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
(r'^api/', include(mobile_api.urls)),
)
和我的 cURL 命令
curl --dump-header - -H "Accept: application/json" -H "Content-Type: application/json" -d"username=vikingosegundo" -d"api_key=12345" -X POST --data "{\"name\":\"arrrg\"}" http://localhost:8000/api/mobile/thing/
响应
{"error_message": "No JSON object could be decoded",
"traceback": "Traceback (most recent call last):\n\n
File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/resources.py\", line 178, in wrapper\n response = callback(request, *args, **kwargs)\n\n
File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/resources.py\", line 379, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n
File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/resources.py\", line 409, in dispatch\n response = method(request, **kwargs)\n\n
File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/resources.py\", line 1077, in post_list\n deserialized = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))\n\n
File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/resources.py\", line 328, in deserialize\n deserialized = self._meta.serializer.deserialize(data, format=request.META.get('CONTENT_TYPE', 'application/json'))\n\n
File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/serializers.py\", line 161, in deserialize\n deserialized = getattr(self, \"from_%s\" % desired_format)(content)\n\n
File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/serializers.py\", line 305, in from_json\n return simplejson.loads(content)\n\n
File \"/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/__init__.py\", line 307, in loads\n return _default_decoder.decode(s)\n\n
File \"/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py\", line 319, in decode\n obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n\n
File \"/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py\", line 338, in raw_decode\n raise ValueError(\"No JSON object could be decoded\")\n\n
ValueError: No JSON object could be decoded\n"
}
我做错了什么? 我如何将 tastypie 指向json 对象? auth+auth 似乎有效。
I am trying to send an ApiKey-autheticated POST request to a tastypie API
my Model:
class Thing(models.Model):
name = models.TextField()
def __unicode__(self):
return u'%s'%self.name
my ModelResource
class ThingResource(ModelResource):
class Meta:
queryset = Thing.objects.all()
resource_name="thing"
authentication = ApiKeyAuthentication()
authorization = DjangoAuthorization()
my urls.py
from django.conf.urls.defaults import patterns, include, url
from tastypie.api import Api
from myapp.api import ThingResource
mobile_api = Api(api_name='mobile')
mobile_api.register(ThingResource())
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
(r'^api/', include(mobile_api.urls)),
)
and my cURL command
curl --dump-header - -H "Accept: application/json" -H "Content-Type: application/json" -d"username=vikingosegundo" -d"api_key=12345" -X POST --data "{\"name\":\"arrrg\"}" http://localhost:8000/api/mobile/thing/
the Response
{"error_message": "No JSON object could be decoded",
"traceback": "Traceback (most recent call last):\n\n
File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/resources.py\", line 178, in wrapper\n response = callback(request, *args, **kwargs)\n\n
File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/resources.py\", line 379, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n
File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/resources.py\", line 409, in dispatch\n response = method(request, **kwargs)\n\n
File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/resources.py\", line 1077, in post_list\n deserialized = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))\n\n
File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/resources.py\", line 328, in deserialize\n deserialized = self._meta.serializer.deserialize(data, format=request.META.get('CONTENT_TYPE', 'application/json'))\n\n
File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/serializers.py\", line 161, in deserialize\n deserialized = getattr(self, \"from_%s\" % desired_format)(content)\n\n
File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/serializers.py\", line 305, in from_json\n return simplejson.loads(content)\n\n
File \"/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/__init__.py\", line 307, in loads\n return _default_decoder.decode(s)\n\n
File \"/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py\", line 319, in decode\n obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n\n
File \"/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py\", line 338, in raw_decode\n raise ValueError(\"No JSON object could be decoded\")\n\n
ValueError: No JSON object could be decoded\n"
}
What am I doing wrong? How do I point tastypie to the json object? The auth+auth seem to be working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在curl 中使用
-d
和--data
会破坏POST 数据。在 GET 中包含
username
和api_key
参数应该可以解决这个问题,如下所示:Using the
-d
and--data
with curl is mangling the POSTed data.Including the
username
andapi_key
params in the GET should solve this, like so:我想自己添加一个答案。我没有将凭据添加为 GET 参数,而是将它们作为自定义 HTTP 标头进行传输,而不是 Josh 的解决方案。
现在我们可以将
X-MYAPP-USERNAME
和X-MYAPP-APIKEY
添加到请求中。这里是一个普通的 telnet 会话,使用 GET
和一个 POST 会话:
并且由于我们还检查 GET 参数以防未提供标头,因此这也有效:
I want to add an answer myself. Instead of Josh's solution I am not adding the credentials as
GET
-parameters, but I transmit them as custom HTTP headers.Now we can add the
X-MYAPP-USERNAME
andX-MYAPP-APIKEY
to the request.Here a plain telnet session, using GET
and a session for POST:
And as we also check the GET-parameters in case no header is provided, this also works: