The dash-dash-libcurl is the sometimes missed curl gem that you might want to know about and occasionally maybe even use.
How do I convert
There is a very common pattern in curl land: a user who is writing an application using language L first does something successfully with the curl command line tool and then the person wants to convert that command line into the program they are writing.
How do you convert this curl command line into a transfer using programming language XYZ?
There is a huge amount of available bindings for libcurl. Bindings, as in adjustments and glue code for languages to use libcurl for Internet transfers so that they do not have to write the application in C.
At a recent count I found more than 60 libcurl bindings. They allow authors of virtually any programming language to do internet transfers using the powers of libcurl.
Most of these bindings are “thin” and follow the same style of the original C API: You create a handle for which you set options and then you do the transfer. This way they are fairly easy to keep up to date with the always-changing and always-improving libcurl releases.
Say hi to
Most curl command line options are mapped one-to-one to an underlying libcurl option so for some time I tried to help users by explaining what they map to. Until one day I realized that
Hey! curl itself already has this mapping in source code, it just needs a way to show it to users!
How would it best output this mapping?
--libcurl command line option was added to curl already back in 2007 and has been been part of the tool since version 7.16.1.
Show this command line as libcurl code
It is really easy to use too.
- Create a complicated curl command line that does what you need it to do.
--libcurl example.cto the command line and run it again.
- Inspect your newly generated file example.c for how you could write your application to do the same thing with libcurl.
If you want to use a libcurl binding rather than the C API, like perhaps write your code in Python or PHP, you need to convert the example code into your programming language but much thanks to the options keeping their names across the different bindings it is usually a trivial task.
The simplest possible command line:
It gets HTTP from the site “curl.se”. If you try it, you will not see anything because it just replies with a redirect to the HTTPS version of the page. But ignoring that, you can convert that action into a libcurl program like this:
curl curl.se --libcurl code.c
The newly created file
code.c now contains a program that you can compile :
gcc -o getit code.c -lcurl
and then run
You might want your program to rather follow the redirect? Maybe even show debug output? Let’s rerun the command line and get a code update:
curl curl.se --verbose --location --libcurl code.c
Now, if you rebuild your program and run it again, it shows you the front page HTML of the curl website on stdout.
The exact code my curl version 7.85.0 produced in the command line above is shown below.
You see several options that are commented out. Those were used by the command line tool but there is no easy or convenient way to show their use in the example. Often you can start out by just skipping those .