Creating C Code from cURL

created Jan 14, 2018

I use cURL often to test my websites and my API code and the code and sites of others. It's a handy utility.

In this Jan 13, 2018 Hacker News thread, I learned something new about cURL.

One thing I am always sure to share with collegues when we discuss curl, is the fact from the command line you can generate the underlying C code.

This is pretty useful when creating a CLI for pretty much any app, and I've used it regularly to generate a CLI for an app.

My post on how to do it:

apt-get update

apt-get install curl

apt-get install libcurl4-openssl-dev

curl --libcurl sawv.c

gcc sawv.c -lcurl -o sawv

./sawv - produces what the cURL command produced, fetching the homepage.

The C code that's created can be used in other applications. It could be a library. This would be helpful for more complex cURL commands that get repeated. Or it's simply interesting because I didn't know that this feature was available in cURL.

And cURL supports the Gopher protocol. I can use it to access my Gopher site.

curl gopher://

gopher:// works too. It points to

The sawv.c code generated by cURL from the above example usage.

/********* Sample code generated by the curl command line tool **********
 * All curl_easy_setopt() options are documented at:

int main(int argc, char *argv[])
  CURLcode ret;
  CURL *hnd = curl_easy_init();
  curl_easy_setopt(hnd, CURLOPT_URL, "");
  curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
  curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/ libidn/1.23 librtmp/2.3");
  curl_easy_setopt(hnd, CURLOPT_SSH_KNOWNHOSTS, "/home/munger/.ssh/known_hosts");
  curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
  ret = curl_easy_perform(hnd);

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

  CURLOPT_WRITEDATA set to a objectpointer
  CURLOPT_WRITEFUNCTION set to a functionpointer
  CURLOPT_READDATA set to a objectpointer
  CURLOPT_READFUNCTION set to a functionpointer
  CURLOPT_SEEKDATA set to a objectpointer
  CURLOPT_SEEKFUNCTION set to a functionpointer
  CURLOPT_ERRORBUFFER set to a objectpointer
  CURLOPT_STDERR set to a objectpointer
  CURLOPT_SOCKOPTFUNCTION set to a functionpointer
  CURLOPT_SOCKOPTDATA set to a objectpointer

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