0
votes

I am using the Request library to test ReST APIs. I am facing a problem while trying to trasform the below cURL to request library Call.

curl https://upload.box.com/api/2.0/files/content \ -H "Authorization: Bearer ACCESS_TOKEN" \ -F filename=@FILE_NAME \ -F parent_id=PARENT_FOLDER_ID

I tried many of the suggestions in this forum. But nothing worked.

The code which I addedd after the comment is:

The code I wrote was:

def upload_a_file(url, folder_id, file_name, access_token): field_values = "{\'filename\': (filename, open("+file_name+", \'rb\'))}" payload = "{\'parent_id\':"+folder_id+"}" request_headers = {'Authorization': 'Bearer '+access_token} result = requests.post(url, headers=request_headers, data=payload, files=field_values) response = result.json() print response

2
Why are you storing payload, etc as strings? See my original example; requests is expecting a dict. docs.python-requests.org/en/latest/user/quickstart/…tufelkinder

2 Answers

1
votes

I assume you mean the requests library?

If so, here is how I do it.

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()
0
votes

I followed the example given in http://www.snip2code.com/Snippet/67408/Show-progress-bar-when-uploading-a-file.

I was able to make a successful API call.