2014-12-19 29 views
0

編輯:python請求授權失敗時,包括json.dumps

如何在python中複製以下curl請求?

curl https://api.box.com/2.0/files/content 
-H "Authorization: Bearer TOKEN" -d '{"name": "Wolves owners.ppt", "parent": {"id": "2841136315"}, "size": 15243}' 
-X OPTIONS` 

當我介紹json.dumps於所述請求授權失敗

以下請求體格式不正確。它只是一個Python字典,嵌套的屬性沒有通過。

h.kwargs['body'] = {"size": 45810, "parent": {"id": "2841136315"}, "name": "83db7037-2037-4f38-8349-255299343e6d"} 

first = requests.options(
     h.kwargs['url'], headers=h.headers, data=h.kwargs['body'] 
    ) 

看體設定爲size=45810&parent=id&name=83db7037-2037-4f38-8349-255299343e6d

報頭中的響應上的授權失敗,一旦401 json.dumps被引入所述第二請求是

`{'Content-Length': '62', 'Content-Type': 'application/x-www-form-urlencoded', 'Accept-Encoding': 'gzip, deflate', 'Authorization': 'Bearer TOKEN', 'Accept': '*/*', 'User-Agent': 'python...'}` 

second = requests.options(
     h.kwargs['url'], headers=h.headers, data=json.dumps(h.kwargs['body']) 
    ) 

標題是

`{'Content-Length': '95', 'Accept': '*/*', 'User-Agent': 'python...', 'Accept-Encoding': 'gzip, deflate', 'Authorization': 'Bearer TOKEN'}` 

我該如何格式化身體以獲得呼叫而不會搞亂授權?

我試過設置爲第二個請求json的內容類型,但它沒有工作

+0

你哪HTTP響應對於其中場被送到成'第一個請求x-www-form-urlencoded'? – Anentropic 2014-12-19 04:01:25

+0

@Antropic'無效值\'size = 75&parent = id&name = 6951cd4f-f037-44da-9710-821dd9089486.csv' – user2954587 2014-12-19 04:03:11

+0

什麼http代碼? – Anentropic 2014-12-19 04:04:48

回答

2

編輯2

我發現,id場是必需的。您的請求沒有id(父文件夾的ID)的值,這就是您收到HTTP狀態400的原因。這裏的API看起來有點奇怪;爲什麼父文件夾ID需要指定兩次?

無論如何,最低要求的字段是parentidnamesize不是必需的,但它們的值將在提供時進行檢查,即名稱驗證和大小與配額和可用存儲相比較。

我還發現,當id字段被省略時,curl和Python會生成相同的400響應。也許你在捲曲測試中包含了id

最後,Content-type標題沒有任何影響。

這是修訂後的Python代碼。

import requests 
import json 

url = 'https://api.box.com/2.0/files/content' 
h.headers = {'Authorization':'Bearer TOKEN'} 
parent_folder_id = "2843500681" # replace with your folder id 

h.kwargs = {"name": "Wolves owners.ppt", 
      "parent": {"id": parent_folder_id}, 
      "id": parent_folder_id, 
      "size": 15243} 

resp = requests.options(url, headers=h.headers, data=json.dumps(h.kwargs)) 

編輯1:問題更新後

要發送相同的請求一樣捲曲:

import requests 

url = 'https://api.box.com/2.0/files/content' 
h.headers = {'Content-type': 'application/x-www-form-urlencoded', 
      'Authorization':'Bearer TOKEN'}  

h.kwargs = {"name": "Wolves owners.ppt", 
      "parent": {"id": "2841136315"}, 
      "size": 15243} 

resp = requests.options(url, headers=h.headers, data=json.dumps(h.kwargs)) 

注意,這明確設置Content-Type頭的application/x-www-form-urlencoded一樣捲曲。否則,請求中沒有顯着差異。

另外請注意,這個請求中的body實際上並不是application/x-www-form-urlencoded,它只是一個字符串。將內容類型設置爲application/json似乎更合適。

這裏是捲曲的請求:

 
OPTIONS /2.0/files/content HTTP/1.1 
User-Agent: curl/7.32.0 
Host: api.box.com 
Accept: */* 
Authorization: Bearer TOKEN 
Content-Length: 76 
Content-Type: application/x-www-form-urlencoded 

{"name": "Wolves owners.ppt", "parent": {"id": "2841136315"}, "size": 15243} 

,這裏是由上面的代碼生成請求:

 
OPTIONS /2.0/files/content HTTP/1.1 
Host: api.box.com 
Content-Length: 76 
Accept-Encoding: gzip, deflate 
Accept: */* 
User-Agent: python-requests/2.5.0 CPython/2.7.5 Linux/3.17.4-200.fc20.x86_64 
Connection: keep-alive 
Content-type: application/x-www-form-urlencoded 
Authorization: Bearer TOKEN 

{"name": "Wolves owners.ppt", "parent": {"id": "2841136315"}, "size": 15243} 
+0

如上所述,我試過這個,它沒有工作 – user2954587 2014-12-19 04:39:38

+0

你didn'不要說你設定了什麼。你是否嘗試過'application/x-www-form-urlencoded'或'application/json'或兩者?無論如何,答案更新。 – mhawke 2014-12-19 04:55:23

+0

你確定curl發送'application/x-www-form-urlencoded'嗎? Box API文檔沒有說明需要發送內容類型標頭 – Anentropic 2014-12-19 05:00:45