2014-10-17 31 views
0

這裏是我的代碼Django的tastypie返回401總是POST方法和行之有效的GET

 class termTypeResource(ModelResource): 
     class Meta: 
      queryset = TermType.objects.all() 
      resource_name = 'gettermtypes' 
      allowed_methods = ['get','post'] 

每當我做捲曲的API像下面

curl --dump-header - -H "Content-Type: application/json" -X POST --data {"termtype":"LONG"}' --apiURL-- 

它返回(注:我不想爲api POST添加驗證或授權)

HTTP/1.0 401 UNAUTHORIZED 
Date: Fri, 17 Oct 2014 12:05:49 GMT 
Server: WSGIServer/0.1 Python/2.7.6 
X-Frame-Options: SAMEORIGIN 
Content-Type: text/html; charset=utf-8 
+0

那麼你的授權頭在哪裏? – coldmind 2014-10-17 12:14:34

+0

@coldmind我已經從API刪除授權,因爲我不需要,即使它然後返回401. – Mithu 2014-10-17 12:17:42

回答

1

從tastypie教程: http://django-tastypie.readthedocs.org/en/latest/tutorial.html#hooking-up-the-resource-s

但是,如果你嘗試發送一個POST/PUT/DELETE的資源,你 發現自己越來越「401未授權」的錯誤。爲了安全起見,Tastypie 隨授權等級(「您允許做什麼」)將 設置爲ReadOnlyAuthorization。這使得在網絡上公開是安全的,但 阻止我們進行POST/PUT/DELETE。讓我們使那些:

authorization = Authorization() 

但你指定以此爲Authorization =,這是錯的。 而且,如何在教程中提到,POST等沒有身份驗證是不安全的。

+0

得到它感謝:) – Mithu 2014-10-17 12:21:54