gpt4 book ai didi

python - 向 tastypie 发送经过身份验证的 POST 请求

转载 作者:太空狗 更新时间:2023-10-30 01:45:11 24 4
gpt4 key购买 nike

我正在尝试向 tastypie API 发送经过 ApiKey 验证的 POST 请求

我的模型:

class Thing(models.Model):
name = models.TextField()

def __unicode__(self):
return u'%s'%self.name

我的模型资源

class ThingResource(ModelResource):
class Meta:
queryset = Thing.objects.all()
resource_name="thing"
authentication = ApiKeyAuthentication()
authorization = DjangoAuthorization()

我的网址.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 似乎在工作。

最佳答案

-d--data 与 curl 一起使用会破坏 POST 数据。

在 GET 中包含 usernameapi_key 参数应该可以解决这个问题,如下所示:

    curl --dump-header - -H "Accept: application/json"\
-H "Content-Type: application/json" -X POST\
--data "{\"name\":\"arrrg\"}"\
"http://localhost:8000/api/mobile/thing/?username=vikingosegundo&api_key=12345"

关于python - 向 tastypie 发送经过身份验证的 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7149866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com