Forum Discussion

AdamHoover's avatar
AdamHoover
New member | Level 1
2 months ago

libcurl upload

I'm trying to upload a file using libcurl.  The code reports an OK upload, but the file does not appear.  This is the equivalent curl command and it works:

curl -X POST https://content.dropboxapi.com/2/files/upload \
 --header "Authorization: Bearer [my token] " \
 --header "Dropbox-API-Arg: {\"mode\":\"add\",\"path\":\"/dummy.txt\"}" \
 --header "Content-Type: application/octet-stream" \
 --data-binary dummy.txt

The following libcurl code executes and seems to work, but the file does not appear on Dropbox. 

fpt_debug=fopen("D:\\trace.txt","wb");
curl_easy_reset(curl_handle);
curl_easy_setopt(curl_handle,CURLOPT_STDERR,fpt_debug);
curl_easy_setopt(curl_handle,CURLOPT_URL,"https://content.dropboxapi.com/2/files/upload");
curl_easy_setopt(curl_handle,CURLOPT_VERBOSE,1);    /* prints info to stderr, for debugging */
curl_easy_setopt(curl_handle,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_2TLS);
curl_headers=NULL;
sprintf(text,"Authorization: Bearer %s",DropboxToken);
curl_headers=curl_slist_append(curl_headers,text);
sprintf(text,"Dropbox-API-Arg: {\"path\":\"%s\"}","/dummy.txt");
curl_headers=curl_slist_append(curl_headers,text);
sprintf(text,"Content-Type: application/octet-stream");
curl_headers=curl_slist_append(curl_headers,text);
curl_easy_setopt(curl_handle,CURLOPT_HTTPHEADER,curl_headers);
fpt=fopen("D:\\dummy.txt","rb");
curl_easy_setopt(curl_handle,CURLOPT_READFUNCTION,read_cb);
curl_easy_setopt(curl_handle,CURLOPT_UPLOAD,1L);
curl_easy_setopt(curl_handle,CURLOPT_URL,"https://content.dropboxapi.com/2/files/upload");
curl_easy_setopt(curl_handle,CURLOPT_READDATA,fpt);
curl_easy_setopt(curl_handle,CURLOPT_INFILESIZE_LARGE,(curl_off_t)(total_bytes);  /* 49 bytes in dummy.txt */
curlret=curl_easy_perform(curl_handle);
fclose(fpt);
curl_slist_free_all(curl_headers);

The call to curl_easy_perform() returns CURLE_OK (0).  The verbose curl output is below.  But the file does not appear in Dropbox.  Any ideas?

  • Host content.dropboxapi.com:443 was resolved.
    * IPv6: 2620:100:601f:14::a27d:90e
    * IPv4: 162.125.9.14
    *   Trying [2620:100:601f:14::a27d:90e]:443...
    *   Trying 162.125.9.14:443...
    * Connected to content.dropboxapi.com (162.125.9.14) port 443
    * schannel: disabled automatic use of client certificate
    * ALPN: curl offers http/1.1
    * ALPN: server accepted http/1.1
    * using HTTP/1.x
    > PUT /2/files/upload HTTP/1.1^M
    Host: content.dropboxapi.com^M
    Accept: */*^M
    Authorization: Bearer [my token]
    Dropbox-API-Arg: {"path":"/WATCH/dummy.txt"}^M
    Content-Type: application/octet-stream^M
    Content-Length: 49^M
    ^M
  • * We are completely uploaded and fine
    * schannel: remote party requests renegotiation
    * schannel: renegotiating SSL/TLS connection
    * schannel: SSL/TLS connection renegotiated
    < HTTP/1.1 404 Not Found^M
    < Content-Length: 1233^M
    < Content-Type: text/html^M
    < Date: Tue, 17 Dec 2024 01:18:46 GMT^M
    < Server: envoy^M
    < Strict-Transport-Security: max-age=31536000; includeSubDomains; preload^M
    < X-Robots-Tag: noindex, nofollow, noimageindex^M
    < Vary: Accept-Encoding^M
    < X-Dropbox-Response-Origin: remote^M
    < X-Dropbox-Request-Id: 4fe8d6981e85466fb0f0c353132e416c^M
    < ^M
    * Connection #0 to host content.dropboxapi.com left intact

 

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    The 404 is from Dropbox and indicates that the request has not successfully reached a Dropbox API endpoint.

    The issue is that you're sending a PUT, but you need to use POST to hit /2/files/upload. Please update your code to use POST and try again.

    • AdamHoover's avatar
      AdamHoover
      New member | Level 1

      That worked, thanks.  For future readers, libcurl defaults to PUT on CURLOPT_UPLOAD, and Dropbox requires POST.  If you follow either libcurl's API description (https://curl.se/libcurl/c/CURLOPT_UPLOAD.html) or example (https://curl.se/libcurl/c/fileupload.html), you must insert this line after setting CURLOPT_UPLOAD:

      curl_easy_setopt(curl, CURLOPT_POST, 1L);

      The CURLOPT_UPLOAD option will reset it back to PUT if you don't order it this way.

       

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,950 PostsLatest Activity: 2 days ago
351 Following

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 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!