Lionel
Lionel

Reputation: 596

unable to post file on usergrid using requests

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')
)

Upvotes: 1

Views: 38

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123400

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)

Upvotes: 2

Related Questions