我正在使用Request库来测试ReST API。我在尝试转换下面的cURL以请求库调用时遇到问题。
卷曲https://upload.box.com/api/2.0/files/content \ -H“授权:持票人ACCESS_TOKEN” -F filename = @ FILE_NAME \ -F parent_id = PARENT_FOLDER_ID
我在这个论坛上尝试了很多建议。但没有任何效果。
我在评论后添加的代码是:
我写的代码是:
def upload_a_file(url, folder_id, file_name, access_token):
field_values =“{\'filename \':(文件名,打开(”+ file_name +“,\'rb \'))}”
payload = "{\'parent_id\':"+folder_id+"}"
request_headers = {'授权':'承载'+ access_token}
result = requests.post(url, headers=request_headers, data=payload, files=field_values)
response = result.json()
print response
答案 0 :(得分:1)
我认为你的意思是requests库?
如果是这样,我就是这样做的。
access_token = <user access token>
filename = <name of the file as you want it to appear on Box>
src_file = the actual file path
parent_id = the id of the folder you want to upload to
headers = { 'Authorization' : 'Bearer {0}'.format(access_token) }
url = 'https://upload.box.com/api/2.0/files/content'
files = { 'filename': (filename, open(src_file,'rb')) }
data = { "parent_id": parent_id }
response = requests.post(url, data=data, files=files, headers=headers)
file_info = response.json()
答案 1 :(得分:0)
我按照http://www.snip2code.com/Snippet/67408/Show-progress-bar-when-uploading-a-file中给出的示例。
我能够成功进行API调用。