curl —libcurl

We actively encourage users to first try out the transfer they want to do with
the curl command-line tool, and once it works roughly the way you want it to,
you append the --libcurl [filename] option to the command line and run it
again.

The --libcurl command-line option will create a C program in the provided
file name. That C program is an application that uses libcurl to run the
transfer you just had the curl command-line tool do. There are some
exceptions and it isn’t always a 100% match, but you will find that it can
serve as an excellent inspiration source for what libcurl options you want or
can use and what additional arguments to provide to them.

If you specify the filename as a single dash, as in --libcurl - you will get
the program written to stdout instead of a file.

As an example, we run a command to just get http://example.com:

  1. curl http://example.com --libcurl example.c

This creates example.c in the current directory, looking similar to this:

  1. /********* Sample code generated by the curl command-line tool **********
  2. * All curl_easy_setopt() options are documented at:
  3. * https://curl.haxx.se/libcurl/c/curl_easy_setopt.html
  4. ************************************************************************/
  5. #include <curl/curl.h>
  6. int main(int argc, char *argv[])
  7. {
  8. CURLcode ret;
  9. CURL *hnd;
  10. hnd = curl_easy_init();
  11. curl_easy_setopt(hnd, CURLOPT_URL, "http://example.com");
  12. curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
  13. curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.45.0");
  14. curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
  15. curl_easy_setopt(hnd, CURLOPT_SSH_KNOWNHOSTS, "/home/daniel/.ssh/known_hosts");
  16. curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
  17. /* Here is a list of options the curl code used that cannot get generated
  18. as source easily. You may select to either not use them or implement
  19. them yourself.
  20. CURLOPT_WRITEDATA set to a objectpointer
  21. CURLOPT_WRITEFUNCTION set to a functionpointer
  22. CURLOPT_READDATA set to a objectpointer
  23. CURLOPT_READFUNCTION set to a functionpointer
  24. CURLOPT_SEEKDATA set to a objectpointer
  25. CURLOPT_SEEKFUNCTION set to a functionpointer
  26. CURLOPT_ERRORBUFFER set to a objectpointer
  27. CURLOPT_STDERR set to a objectpointer
  28. CURLOPT_HEADERFUNCTION set to a functionpointer
  29. CURLOPT_HEADERDATA set to a objectpointer
  30. */
  31. ret = curl_easy_perform(hnd);
  32. curl_easy_cleanup(hnd);
  33. hnd = NULL;
  34. return (int)ret;
  35. }
  36. /**** End of sample code ****/