Our Community is in read-only mode until April 8th, learn more here. You can still search existing threads or get help via Dropbox Support, the Dropbox Help Center, or Learn.
Forum Discussion
hb_
6 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 ...
hb_
6 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");
Здравко
6 years agoLegendary | Level 20
It's error to pass ifstream object as char sequence! You have to read the file content. :wink:
About Dropbox API Support and Feedback
Get help with the Dropbox API from fellow developers and experts.
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!