cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to know what we learned at IBC? Check out our learnings on media, remote working and more right here.

Discuss Dropbox Developer & API

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to upload file via Curl with Binary object input?

How to upload file via Curl with Binary object input?

Amicao
Explorer | Level 3

Hello, good evening for all. I would like to know, how I can to upload file using a Curl request, or PHP Curl request, with the upload file request.

But instead I use the parameter o filepath, I use the binary object inside body request, for example, some 010101101011010101010 binary code inside request instead of "/folder/filename.extension" . Because I am building a system client-server that a user can upload a file, sending by Post request to server, and the server must to take this file via $_ENV[] and insert it into a request to upload to Dropbox. And doesnt a way to server save temporary file inside your filesystem because the your provider is ephemeral, and so, because it i have to insert the file binary data inside request.

 

I will be grateful.

4 Replies 4

Greg-DB
Dropbox Staff

[Cross-linking for reference: https://stackoverflow.com/questions/72758417/how-to-upload-file-using-an-array-with-binary-data-file... ]

 

When uploading a file to Dropbox using the Dropbox API /2/files/upload endpoint, the app needs to supply several different types of things, including:

  • the parameters for the upload call, documented under "UploadArg" and sent in the "Dropbox-API-Arg" request header: this includes a "path" parameter that tells Dropbox where Dropbox account you want to put the uploaded file. Refer to the documentation for your network client for information on how to set request headers.
  • the bytes of the file data to upload, sent in the request body: this should be the exact file contents that you want to put in the connected Dropbox account at the path specified as noted above. Exactly how you retrieve the file data and put it in the request body will depend on the specific of your scenario. Refer to the documentation for your network client for information on how to set the request body.

For example, using curl on the command line, this would upload a file to the remote path "/test_605632.txt" in Dropbox, telling curl to send the contents of the local file at the local (relative) path "test/test.txt" (the "@" is a way of telling curl to read from the local filesystem):

curl -X POST https://content.dropboxapi.com/2/files/upload \
    --header "Authorization: Bearer <ACCESS_TOKEN>" \
    --header "Dropbox-API-Arg: {\"path\":\"/test_605632.txt\"}" \
    --header "Content-Type: application/octet-stream" \
    --data-binary @test/test.txt

And for comparison, this would upload a file to the remote path "/test_605632.txt" in Dropbox, telling curl to send the data "some data" itself (not from a local file):

curl -X POST https://content.dropboxapi.com/2/files/upload \
    --header "Authorization: Bearer <ACCESS_TOKEN>" \
    --header "Dropbox-API-Arg: {\"path\":\"/test_605632.txt\"}" \
    --header "Content-Type: application/octet-stream" \
    --data-binary "some data"

 

karanag
Explorer | Level 4

Hi,

 

I am facing kind of the same problem. My Code is below but it uploading a corrupted image, any ideas what exactly I am doing wrong? I am trying to build my app using no-code tool AppGyver

 

const imagePath = inputs.imagePath;  // (This line takes the path of the image)

var path = "/Pictures/testPicturreABC12345.jpg";
var content = "@" + imagePath;
var accessToken = "sl.BLX6BjKTDsrOpArs74Bk32Pg09Ukns0u3YUCsDnoagiOJdx2hq6iCZezCKlWc-53v8E6IVOaU3Tu0OAOGMrxc5KIVeihAn51f5hZ0x3EKpCJdEKuykWIz4SAeC4WcjXpKiSrhWC8";
var result;

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
result = xhr.responseText;
return result;
}
};
xhr.open("POST", uploadUrl, true);
xhr.setRequestHeader("Authorization", "Bearer " + accessToken);
xhr.setRequestHeader("Content-type", "application/octet-stream");
xhr.setRequestHeader("Dropbox-API-Arg", '{"path": "' + path + '"}');
xhr.send(content);

Greg-DB
Dropbox Staff

@karanag In your code, you're sending the data in your 'content' variable, which just contains the string '"@" + imagePath', not the actual file data at that path. In the examples in the Dropbox API documentation, the "@" is a way of telling curl on the command line to read from the local filesystem at that path. That wouldn't necessarily work in other environments, such as using XMLHttpRequest like you are here. You'll need to update your code to pass in the actual file data. Please refer to the documentation for your platform for information on accessing file data.

karanag
Explorer | Level 4

Ahh, thanks! Will do a deep dive into the documentation

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    karanag Explorer | Level 4
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?