Should you use curl through commandline or include the library?

Viewed 441

This one i feel is hard to justify one over another. I see so many here at stackoverflow do both the variants. Some just call cURL console commmands externally from their programs(example: curl "content=hello world" -X POST https://example.com -H "charset=UTF-8"). And others import the library and call it programmatically inside the program.

So my question is for upcoming projects in the future, is it worth the work to include the library and setup all of it. Or is it simply a smarter option to call cURL from command line and read it's responses?

And out of curiosity why doesn't everyone do one of the variants? Why do some people use console commands and other include the libraries, are there maybe some pitfalls later down the line?

3 Answers

Whenever possible use the library. Not just for cURL, but in general.

You should definitely avoid system() calls or creating separate processes launching the external command line application. This adds overhead and can cause issues when porting your code between different platforms.

Also libcurl has a ton of features. The command line is extensive, but the C API is even more powerful.

Using libcurl will require more lines of code than calling the curl command line application, but it's the right thing to do.

Calling the curl command line application from C is usually done out of lazyness or as a shortcut to save some time.

libcurl has extensive features that can't easily be expressed as a command line. You would prefer it if you're building something that needs to efficiently handle many transfers at once, or if you need advanced features like receiving HTTP2 pushed resources.

curl is easier to use. You would prefer it if you just need to download or upload a few things.

FYI You can pass the --libcurl argument to curl to see how a curl command line would be implemented in libcurl.

I used to have the same concern about whether to use libcurl or simply to invoke command-line.

Command-line tool curl is relatively simple to use. But a child process is spawned and then you need IPC if data exchanging is required between parent and child process.

While, using libcurl is harder since you are entailed to reading API references and picking up functions you need then arranging them to work. But the payoff is that libcurl is more flexible, more powerful.

My experience is that I used system(curl) to send JSON data in a C++ program. That program was a single-way communication. By using curl command line, I quickly finished what I expected. But if I chose to use libcurl, I believe I would read the docs for a few days before writing a single line of code.

There is another story of mine. It is same to ask whether to use ffmpeg tool or its library. My project is to extract frames from videos and then to feed them into other subroutines. So I used libavcodec/libavformat or so to do my job. This did cost a few more effort but good thing was reading tutorials on the internet sufficed.

Related