Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
I am trying to figure out how to upload a file on an ESP32C3 [which uses the Arduino IDE] to Dropbox. I am using the generic HTTP API as a basis for
doing so. I am effectively converting "curl" commands to Arduino code.
But sods law, I was stopped at the last post. I have list files working, and I have download files working. But uploading, the most important task, is giving me some sort of generic error.
I am trying to convert this line.
curl -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer sl.B9dZuDVGsJYG3iabZAIXdK4mzhVom0gdruvL3rHuc4RuxE_BnDlVtjc1lGrrezNGrPqmVNDMXJ5ssKPptz5JNk7Q_MBYZ9pbLdPxmr85Nk3TV0umh9OlivoAjynA9VC6LHrpib2A__8vt1lG6wfST4E" \
--header "Dropbox-API-Arg: {\"autorename\":false,\"mode\":\"add\",\"mute\":false,\"path\":\"/Homework/math/NewImage.jpg\",\"strict_conflict\":false}" \
--header "Content-Type: application/octet-stream" \
--data-binary _5050.jpg
I wrote this in code. The message is generated by the headers I am sending; I don't get to the actual upload of the file, although I am flying by the seat of my pants here. Has anybody done this? Is there something obviously wrong here?
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Dropbox - 400</title>
<link href="https://cfl.dropboxstatic.com/static/metaserver/static/css/error.css" rel="stylesheet" type="text/css"/>
<link rel="shortcut icon" href="https://cfl.dropboxstatic.com/static/images/favicon.ico"/>
</head>
<body>
<div class="figure">
<img src="https://assets.dropbox.com/www/en-us/illustrations/spot/target-miss.svg" alt="Error: 400"/>
</div>
<div id="errorbox">
<h1>Error (400)</h1>Something went wrong. Don't worry, your files are still safe and the Dropbox team has been notified. Check out our <a href="https://status.dropbox.com">Status Page</a> to see if there is a known incident, our <a href="https://www.dropbox.com/help">Help Center</a> and <a href="https://forums.dropbox.com">forums</a> for help, or head back to <a href="https://www.dropbox.com/home">home</a>.
</div>
</body>
</html>
I figured out what I was doing wrong and posted the working code for the community's benefit.
void uploadFile() {
Serial.println("Running Upload_file: ");
HTTPClient http;
File file = SD.open("/240x320.jpg");
if(!file) {
Serial.println("Failed to open file for reading");
return;
}
int len = file.size();
char size[8];
sprintf(size, "%d", len);
static uint8_t buff2[27142] = {255};
static unsigned char encoded[27142] = {255};
int x = file.read(buff2,len);
unsigned int base64_length = encode_base64(buff2, len + 1, encoded);
close(file);
Serial.print("Size ");
Serial.println(size);
if (http.begin(dropbox_uploadfile)) { // HTTP
String token_key = String("Bearer ") + dropbox_token;
http.addHeader("Authorization", token_key);
String args = "{\"autorename\":false,\"mode\":\"add\",\"mute\":false,\"path\":\"/Homework/math/Uploaded8.txt\",\"strict_conflict\":false}";
http.addHeader("Dropbox-API-Arg",args);
String arg2 = "application/octet-stream";
http.addHeader("Content-Type",arg2);
String wild = "The luck of the Irish";
int httpCode = http.POST((char*)encoded);
String payload = http.getString();
Serial.print("payload: ");
Serial.println(payload);
http.end();
}
}
I posted an article on Medium with the download and list files commands, too, which you can find at this link. Yes, it's behind a paywall, but the first three articles are free.
https://medium.com/me/stats/post/1f755145f504
Yes, a generic 400 response to an API call like that generally means that the call failed because the servers could not parse the request as a valid HTTP request, indicating that something about the request was malformed.
Are you able to enable any sort of verbose mode so you can view the raw HTTP request? If possible, that may be helpful for debugging this.
I figured out why it didn't like the headers, it was the spaces: but I got a new problem. After sending in the request I use a stream to upload the data. Sadly it seems to ignore said stream...
Do you have any more thoughts on this?
Thanks for following up. I'm glad to hear you resolved that first issue.
Can you clarify what you mean when you say "it simply doesn't upload anything" though? What response do you get now?
By the way, I notice you have a password and access token in your message. Please keep in mind this thread is public, so be sure to redact any sensitive values.
I accidently posted the entire program, which immediately changed. I am trying to upload a file here. It doesn't upload anything. It doesn't even create an empty file.
Another baby step; it creates a file and puts the options I stipulated inside it. Here is the almost working code.
Code deleted, answer at the end of this thread
And I get txt file that says this on my dropbox.
{"autorename":false,"mode":"add","mute":false,"path":"/Homework/math/Uploaded2.jpg","strict_conflict":false}
While I can't provide support for your HTTPClient class itself, from your description and code, it seems like the 'http.POST(args)' line POSTs the argument data into the request body, which is where the file data is supposed to go, so that becomes the uploaded file data. You may need to refer to the documentation for your HTTP client for more information on how to configure that to handle your use case.
I figured out what I was doing wrong and posted the working code for the community's benefit.
void uploadFile() {
Serial.println("Running Upload_file: ");
HTTPClient http;
File file = SD.open("/240x320.jpg");
if(!file) {
Serial.println("Failed to open file for reading");
return;
}
int len = file.size();
char size[8];
sprintf(size, "%d", len);
static uint8_t buff2[27142] = {255};
static unsigned char encoded[27142] = {255};
int x = file.read(buff2,len);
unsigned int base64_length = encode_base64(buff2, len + 1, encoded);
close(file);
Serial.print("Size ");
Serial.println(size);
if (http.begin(dropbox_uploadfile)) { // HTTP
String token_key = String("Bearer ") + dropbox_token;
http.addHeader("Authorization", token_key);
String args = "{\"autorename\":false,\"mode\":\"add\",\"mute\":false,\"path\":\"/Homework/math/Uploaded8.txt\",\"strict_conflict\":false}";
http.addHeader("Dropbox-API-Arg",args);
String arg2 = "application/octet-stream";
http.addHeader("Content-Type",arg2);
String wild = "The luck of the Irish";
int httpCode = http.POST((char*)encoded);
String payload = http.getString();
Serial.print("payload: ");
Serial.println(payload);
http.end();
}
}
I posted an article on Medium with the download and list files commands, too, which you can find at this link. Yes, it's behind a paywall, but the first three articles are free.
https://medium.com/me/stats/post/1f755145f504
Hi there!
If you need more help you can view your support options (expected response time for a ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!