1

I am able to post file with 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'

But when I tried same thing with requests.post, file didn't upload to server. Does anybody know why this happen.

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')
)
brandonscript
  • 68,675
  • 32
  • 163
  • 220
Lionel
  • 604
  • 9
  • 26

1 Answers1

2

You appear to be missing the name field, add that to your files dictionary, or to a new data dictionary (either is fine). Your file should be named 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)

or

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)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I include name in `files` dict, but still file don't upload... I'm able to upload file with above mention curl request.. – Lionel Sep 23 '15 at 10:25
  • @Pattinson: ah, my mistake, I missed that you named the field `memo.txt`; the field is named `file` in the curl request. – Martijn Pieters Sep 23 '15 at 10:26