Unable to Upload Files using API and Curl
Hello,
I am attempting to just do a basic upload of a single 102KB file named Photo.jpg using curl on Windows 10. I have the free account for now and created a custom app with "Standard OAuth 2.0 (User Authentication)" for authentication. The user is the Co-Admin.
For the token, I am currently just trying to get it to work with the "Developer Token" which I know is only active for 60 minutes.
Here is the syntax I am using:
curl -X POST https://api.box.com/2.0/files/content -H "Authorization: Bearer DEVTOKEN" -H "Content-Type:multipart/form-data" -F attributes="{"name":"Photo.jpg", "parent":{"id":"11446498"}}" -F file=@Photo.jpg
When I enter this, it just goes to the next line. I check my Box folder and there is nothing uploaded. I also tried to change the folder ID to "0" but that did not work either to place file on the root directory.
Any suggestions? I am desperate as I need to get this working soon for a work deadline to see if we want to change providers to Box.
Thank You!
Edit: Also, if I try the original syntax that is found on the Upload Files help page: then I get the following error:
C:\Users\Ahmed\Downloads\curl-7.72.0_2-win64-mingw\curl-7.72.0-win64-mingw\bin>curl -X POST https://api.box.com/2.0/files/content -H "Authorization: Bearer DEVTOKEN" -H "Content-Type:multipart/form-data" -F attributes='{"name":"Photo.jpg", "parent":{"id":"11446498"}}' -F file=@Photo.jpg
Error:
curl: (3) unmatched close brace/bracket in URL position 20:
parent:{id:11446498}}'
^
Thus that is why I changed the single quotation (') to double quotes (") but then stuck on what to do from here.
Edit 2: I have also tried to do it in Linux, and same thing where it will be blank:
curl -X POST https://api.box.com/2.0/files/content \
-H 'Authorization: Bearer DEVTOKEN' \
-H "Content-Type: multipart/form-data" \
-F attributes='{"name":"Photo.jpg", "parent":{"id":"121466666995"}}' \
-F file=@Photo.jpg
I am running the commands from the folder that has the file I want to upload to. I am stumped.
-
Official comment
I am so sorry, it seems we had a mistake in our cURL samples for uploads.
The correct endpoint for uploads is on a different domain. I've updated the samples
curl -i -X POST https://upload.box.com/api/2.0/files/content \
-H 'Authorization: Bearer <TOKEN>' \
-H "Content-Type: multipart/form-data" \
-F attributes='{"name":"file.txt", "parent":{"id":"1234"}}' \
-F file=@file.txtAs for the issue you encounter with double/single quotes: behaviour of cURL between Linux/Mac/Windows is slightly different. You can choose to either use single quotes on the outside, and then use double quotes in the JSON, or use double quotes around the whole string and then use escaped double quotes in the JSON. For example:
-F attributes="{\"name\":\"file.txt\", \"parent\":{\"id\":\"1234\"}}"
I also recommend adding -i to your API calls so you can see the headers returned by the API, which is useful when the API returns an error.
Comment actions
Please sign in to leave a comment.
Comments
5 comments