0

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

Deepti K
  • 590
  • 2
  • 9
  • 26
  • Why are you storing payload, etc as strings? See my original example; requests is expecting a dict. http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests – tufelkinder Sep 21 '14 at 01:41

2 Answers2

1

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()
tufelkinder
  • 1,176
  • 1
  • 15
  • 37
  • Can you provide any more details...? Are you using the requests package? Have you authenticated your app? I will modify the above to clarify the variables. – tufelkinder Sep 20 '14 at 21:20
0

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.

Deepti K
  • 590
  • 2
  • 9
  • 26