无法使用请求在usergrid上发布文件

时间:2015-09-23 10:02:45

标签: file-upload python-requests usergrid

我可以使用curl

发布文件
curl -X POST -i -F name='memo.txt' -F file=@/home/tester/Desktop/memo.txt  
'http://localhost:8080/***/***/concerts/008?
access_token=YWMtraI2DF21EeWx_Rm4tdnmrwAAAVACjcyGG8TpdXJBXdjnRJ2SeqIAZI1T8Xk'

但是当我用requests.post尝试同样的事情时,文件没有上传到服务器。有谁知道为什么会这样。

import requests

url = 'http://localhost:8080/***/***/concerts/008'
files = {
    'memo.txt': open('/home/tester/Desktop/memo.txt', 'rb'),
    'name': 'memo.txt'
}
r = requests.post(
    url, files=files, 
    params=dict(access_token='YWMtraI2DF21EeWx_Rm4tdnmrwAAAVACjcyGG8TpdXJBXdjnRJ2SeqIAZI1T8Xk')
)

1 个答案:

答案 0 :(得分:2)

您似乎缺少name字段,将其添加到您的files字典或新的data字典(两者都可以)。您的文件应命名为file

import requests

url = 'http://localhost:8080/***/***/concerts/008'
files = {'file': open('/home/tester/Desktop/memo.txt','rb')}
data = {'name': 'memo.txt'}
params = {'access_token': 'YWMtraI2DF21EeWx_Rm4tdnmrwAAAVACjcyGG8TpdXJBXdjnRJ2SeqIAZI1T8Xk'}
r = requests.post(url, data=data, files=files, params=params)

import requests

url = 'http://localhost:8080/***/***/concerts/008'
files = {
    'file': open('/home/tester/Desktop/memo.txt','rb'),
    'name': 'memo.txt'
}
params = {'access_token': 'YWMtraI2DF21EeWx_Rm4tdnmrwAAAVACjcyGG8TpdXJBXdjnRJ2SeqIAZI1T8Xk'}
r = requests.post(url, files=files, params=params)