Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
hb_
7 years agoHelpful | Level 5
Upload a file using c++ via curl
Hi, I was googling for a way to upload a file to any cloud storage using c++ and came across dropbox api. I found the code, so I did a copy and paste to try and see if it works. I entered the Access ...
Greg-DB
Dropbox Community Moderator
7 years agoЗдравко Thanks for helping out! I can't speak to why any particular language was or wasn't supported in an official SDK, but I'll pass this along as a feature request for an official C/C++ Dropbox SDK. I can't promise if or when that might be implemented though.
hb_ If you're still having trouble, please share the code you have so far so we can take a look.
For reference, when uploading a file to Dropbox via the Dropbox API /2/files/upload endpoint, the data for the file you want to upload needs to be sent in the request body. For example, you might do something like this:
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "data for upload here");
To get the data you want to upload, you can use whatever local file I/O access you have to read the data from the local file.
hb_
7 years agoHelpful | Level 5
Hi Greg-DB , thanks for replying so quick
I followed your suggestion and tried tp use std::ifstream like this:
CURL *curl;
CURLcode res;
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
string test = "D:\\Desktop\\test.txt";
std::ifstream file(test);
if(curl)
{
printf ("Running curl test.\n");
struct curl_slist *headers=NULL; /* init to NULL is important */
headers = curl_slist_append(headers, "Authorization: Bearer <my_access_token>");
headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
headers = curl_slist_append(headers, "Dropbox-API-Arg: {\"path\":\"/test.txt\"}");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, "https://content.dropboxapi.com/2/files/upload");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, file);
curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, FALSE);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
printf ("\nFinished curl test.\n");
}
curl_global_cleanup();
printf ("Done!\n");but it didn't compile displaying this error:
C:\Min.GW\curl-7.64.1-win64-mingw\include\curl\curl.h|2857|error: use of deleted function 'std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const std::basic_ifstream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]'|
But before that I had this code:
CURL *curl;
CURLcode res;
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if(curl)
{
printf ("Running curl test.\n");
struct curl_slist *headers=NULL; /* init to NULL is important */
headers = curl_slist_append(headers, "Authorization: Bearer <my_access_token>");
headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
headers = curl_slist_append(headers, "Dropbox-API-Arg: {\"path\":\"/D:\\Desktop\\test.txt\"}");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, "https://content.dropboxapi.com/2/files/upload");
curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, FALSE);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
printf ("\nFinished curl test.\n");
}
curl_global_cleanup();
printf ("Done!\n");And this would just create new folders and files in my account
Don't know if the codes I posted might be too long, the difference between the two is only at this line:
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "test data for upload");
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
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!