alexcrichton / curl-rust

Rust bindings to libcurl
MIT License
1k stars 234 forks source link

How upload file, just like curl -T <file> #560

Open zhang-huaimin opened 1 month ago

zhang-huaimin commented 1 month ago

Hello, I often use curl -T file in bash to upload file, how should this be done in rust-curl? Hope your reply, thanks!

sagebind commented 1 week ago

curl-rust is a thin wrapper over libcurl so generally examples out in the wild using the libcurl API in most any language should translate to some degree to curl-rust. (Note that the curl command is a application that uses libcurl but adds its own code on top. curl-rust has no interaction with the curl command.)

The curl command actually has a neat trick to show you how one might achieve similar results / options as any given curl command using libcurl by appending --libcurl to the command. For example, curl -T file.txt http://example.com --libcurl - will show you the following C code:

/********* Sample code generated by the curl command line tool **********
 * All curl_easy_setopt() options are documented at:
 * https://curl.se/libcurl/c/curl_easy_setopt.html
 ************************************************************************/
#include <curl/curl.h>

int main(int argc, char *argv[])
{
  CURLcode ret;
  CURL *hnd;

  hnd = curl_easy_init();
  curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, 102400L);
  curl_easy_setopt(hnd, CURLOPT_URL, "http://example.com/file.txt");
  curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
  curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
  curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/8.2.1");
  curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
  curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2TLS);
  curl_easy_setopt(hnd, CURLOPT_FTP_SKIP_PASV_IP, 1L);
  curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);

  /* Here is a list of options the curl code used that cannot get generated
     as source easily. You may choose to either not use them or implement
     them yourself.

  CURLOPT_WRITEDATA was set to an object pointer
  CURLOPT_WRITEFUNCTION was set to a function pointer
  CURLOPT_READDATA was set to an object pointer
  CURLOPT_READFUNCTION was set to a function pointer
  CURLOPT_SEEKDATA was set to an object pointer
  CURLOPT_SEEKFUNCTION was set to a function pointer
  CURLOPT_ERRORBUFFER was set to an object pointer
  CURLOPT_STDERR was set to an object pointer
  CURLOPT_HEADERFUNCTION was set to a function pointer
  CURLOPT_HEADERDATA was set to an object pointer

  */

  ret = curl_easy_perform(hnd);

  curl_easy_cleanup(hnd);
  hnd = NULL;

  return (int)ret;
}
/**** End of sample code ****/

Usually to upload a file you'd open the file and then set CURLOPT_READFUNCTION to a function that would read a chunk of the file and copy it into libcurl's upload buffer. One curl-rust wrapper for this option is read_function.