cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Tell us what you want to see on the Community here!
Close

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

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

Re: Upload a file using c++ via curl

Highlighted

Upload a file using c++ via curl

Helpful | Level 5

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 token and ran the code, the upload failed showing this error:

"Error in call to API function "files/upload": The given OAuth 2 access token is malformed."

I suspect the error could be related to this line of code:

curl_easy_setopt(curl, CURLOPT_URL, "https://content.dropboxapi.com/2/files/upload");

But I am new in this so I have no idea how to solve it.

23 Replies 23
Highlighted

Re: Upload a file using c++ via curl

Super Collaborator | Level 20

Hi @hb_,

Definitely the problem is not in cited code line. Did you set properly CURLOPT_XOAUTH2_BEARER parameter?

Hope this gives some direction.

Highlighted

Re: Upload a file using c++ via curl

Helpful | Level 5

@Здравко  Thank you. You were right, I guess this time I set CURLOPT_XOAUTH2_BEARER parameter properly so it gave another error:

"Error in call to API function "files/upload": Must provide HTTP header "Authorization" or URL parameter "authorization"."

I am searching to find out what this error could be about. 

If you can help in any way I would much appreciate.

Highlighted

Re: Upload a file using c++ via curl

Helpful | Level 5

hey @Здравко  I think it worked

However I came to find that the code does not do what I actually want, what it does is just creates a new file and write something you specify to it but I want to upload a local file from my computer to my dropbox account?

Any idea how can I do that?

Highlighted

Re: Upload a file using c++ via curl

Super Collaborator | Level 20

Hey @hb_,

Seems you have some progress. Most upload operations (no restricted to Dropbox) are POST based. It would be difficult (and impractical) to encode big file's content to GET parameters! Further, every content is transferred as... content and you have to direct where this content to be placed on. The target file name could be the same as source file name (if any) or different.  The choice is yours! I'm not sure where you read reference info for CURL, but curl.haxx.se is good starting point and together with Dropbox API (also SDK) documents you can do what you want. Between: libcurl is C library, not C++ (as in your thread header).

Of course would be nice if there was Dropbox C/C++ SDK. It's really curious why 'fundamental' languages C and  C++ are left without proper support from Dropbox side. There are many libraries providing underlying transport support that could be used as base for Dropbox 'shell'. For example Qt, WxWidgets, Boost and many others. Of course libcurl, discussed here, is also good example. This thread could be a proposal! @Greg-DB, What you would say about?

Highlighted

Re: Upload a file using c++ via curl

Dropboxer

@Здравко 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.

Re: Upload a file using c++ via curl

Helpful | 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");
Highlighted

Re: Upload a file using c++ via curl

Super Collaborator | Level 20

It's error to pass ifstream object as char sequence! You have to read the file content.

Highlighted

Re: Upload a file using c++ via curl

Helpful | Level 5

@Здравко yeah

you are right I can read a text file content into a char and pass it as a parameter but what aboout a audio file or a video?

Highlighted

Re: Upload a file using c++ via curl

Super Collaborator | Level 20

The same. In addition you can use CURLOPT_POSTFIELDSIZE (you can't rely on zero termination).

Good example could be foud here.

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Need more support?