Box Upload API loading the file but the file is unreadable and has no size displayed too
AnsweredBox Upload API is loading file using Python code , but the file is unreadable.
The file loaded using Postman is readable. The problem happens only with the file loaded using Python code.
The only difference between Postman and Python code is , I am using "verify:false" in order to escape the SSL cert problem.
Please help me on what I am doing wrong. I am attaching the Python code below
import requests url = "https://upload.box.com/api/2.0/files/content" payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"attributes\"\r\n\r\n{\"name\":\"TestData.xlsx\", \"parent\": {\"id\": \"***number removed for privacy***\"}}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\Users\\ABCD\\TestData.xlsx\"\r\nContent-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--" headers = { 'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", 'Authorization': "Bearer ABCDEFGHIJKLMNOPQRSTUV", 'Cache-Control': "no-cache" } response = requests.request("POST", url, data=payload, headers=headers, verify=False) print(response.text)
-
Based on the code you included, it doesn't look like you're actually including the contents of the file in the payload — the string you pasted just has an empty line for the `file` part of the multipart form body. You'll need to actually add the file contents to upload them. Have you tried using our Python SDK? It has methods available to do file upload for you!
-
. Thanks for your answer I don't see the upload functionality here --> https://github.com/box/box-python-sdk#usage
I don't see the BoxSDK in Anaconda package list too. Is the sdk not available via Anaconda ?
-
, I have already written a lot of code with Python requests library and I have to refactor a lot of code if I have to switch to Box SDK. So I thought of sticking to Python requests library for now
The following question may be naive. But I am kind of stuck with uploading the file.
The following payload at least loads a empty file
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n Content-Disposition: form-data; name=\"attributes\"\r\n\r\n {\"name\":\"XYZ.xlsx\", \"parent\": {\"id\": \"XYZ\"}}\r\n ------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n Content-Disposition: form-data; name=\"file\"; filename=\"C:\\XYZ\\XYZ.xlsx\"\r\n Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n\r\n\r\n ------WebKitFormBoundary7MA4YWxkTrZu0gW--"
But the below payload returns 400 error. I am trying to send the file contents in this payload. What am I doing wrong here ?
payload = { 'Content-Disposition': 'form-data', 'attributes': {'name': 'XYZ.xlsx', 'parent': {'id': 'XYZ'}}, 'file': {'name': open('C:\\XYZ\\XYZ.xlsx','rb'), 'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'} }
-
Have you checked out the requests library documentation on POSTing files via a multi-part form? You should be able to follow that pattern to pass a file stream to requests for the file part — manually constructing the payload string is probably not advisable in this case. You could take a look at what the Box Python SDK is doing to get an idea of one way this could work. In general, it looks like you may want to use the MultipartEncoder from requests toolbelt to construct a multipart form containing both the file attributes JSON and the file contents.
-
, Java SDK for Box is far more easier than Python SDK. I switched to Java SDK.
I hope the Python SDK becomes more developer friendly with clear examples like Java SDK --> https://github.com/box/box-java-sdk
-
I'm really sorry you weren't able to get things working with Python! You're correct though, our Java SDK is currently much better documented. My team will actually be taking on a project to improve the Python SDK and its documentation in the next few months, so hopefully we can make the situation a bit better going forward.
-
+1, I am familiar to AWS S3 upload with boto and they have two distinct methods :
1 for the multipart upload with a queue management for multithreading and the other one for the simple single file upload.
The right url for Java is here : https://github.com/box/box-java-sdk/blob/master/doc/files.md#upload-a-large-file-in-chunks
Maybe I can have a look at it.
Please sign in to leave a comment.
Comments
7 comments