I've been trying to upload a file using the box v2 api with requests.
So far I had little luck though. Maybe someone here can help me see what I'm actually doing wrong.
file_name = "%s%s" % (slugify(sync_file.description), file_suffix)
file_handle = open(settings.MEDIA_ROOT + str(sync_file.document), 'rb')
folder_id = str(sync_file.patient.box_patient_folder_id)
r = requests.post(
files_url,
headers=headers,
files={
file_name: file_handle,
"folder_id": folder_id,
},
)
My authentication works, because I'm creating a folder just before that, using the same data.
A response looks something like this:
{
u'status': 404,
u'code': u'not_found',
u'help_url': u'http://developers.box.com/docs/#errors',
u'request_id': u'77019510950608f791a0c1',
u'message': u'Not Found',
u'type': u'error'
}
Maybe someone on here ran into a similar issue.
My solution, using requests:
It would be nice if you could specify the
new_copy
parameter but there's nothing documented for it and it doesn't seem to work.You need to pass 2 Python dictionaries, files and data. files are
{uniqFileName:openFileObj}
, and data are{uniqFileName:filename}
. Below is the upload method from my box class. And remember to add a final entry in data,'folder_id': destination_id
.Here is a sample call:
Like I said, the above function is a method of a class, with an instance attr that holds a requests session. But you can use
requests.post
instead ofself.session.post
, and it will work just the same. Just remember to add the headers with your apikey and token if you do it outside a session.According to the documentation, you are supposed to be able to rename the file by giving it a new name in the data dict. But I can't make this work except by copying the src file to a temp dir with the desired name and uploading that. It's a bit of a hack, but it works.
good luck, Mike
As someone requested my implementation, I figured I would put it out here for anyone trying to achieve something similar.
So in essence, I needed to send the file and retrieve the ID of the newly updated file and send another request to box. Personally, I don't like it either, but it works for me and haven't been able to find any other implementations that do the correct naming from the get-go.
Hope someone can benefit from this snippet.