我可以使用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')
)
答案 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)