Curl Overview

Curl (stands for Client URL) is a software tool that provides a command-line tool (Curl) and a library (libcurl) for transferring data in various formats between a Curl client and a web server. Curl supports several network protocols, including HTTP, HTTPS, FTP, and SFTP, and can work without user interaction and is widely used in automation scripts. In addition, Curl offers many useful options such as proxy support, user authentication, resume download, cookie support, web form submission, and more.

What are the benefits of using Curl?

  • Curl is portable and compatible with all popular operating systems.
  • Curl can work without user interaction and can be used in automation scripts.
  • Curl can provide detailed logs of what was sent and received and is very good at debugging and logging errors.
  • Curl can set rate limits for both uploads and downloads.
  • Curl is very fast and can simultaneously download data from multiple sources.
  • Curl is very flexible, and each Curl parameter can be configured, such as download speed or connection timeout.

What is libcurl?

libcurl is a free open source library that can add all the same capabilities to your application as the Curl command-line tool. libcurl has an excellent distribution license agreement and can be used in commercial or closed source applications; therefore, it is widely used in many popular applications. libcurl supports HTTP / 1.1, HTTP / 2, HTTP / 3, HTTPS, FTP and FTPS protocols, web form submission, file uploads, cookies, user authentication, proxies, and rate limits.

libcurl is thread-safe, IPv6 compatible, highly portable, and can be used on many platforms including Windows, Linux, and macOS, and can be compiled directly into C and C ++ applications and has bindings for many popular programming languages, including PHP, Python, Ruby, and others.

Can Curl be used to test and document APIs?

Because the Curl command-line tool can be easily used in automation scripts, it makes Curl a convenient and widely used tool for testing and documenting API calls. With the Curl command-line tool, you can send a JSON to the REST API endpoint, submit a web form to a server, load test an URL, upload a file, set TCP/IP connection timeouts, and more. Developers can copy and paste Curl commands from the API documentation and run those commands on any development platform without adapting them to their platform. Each API request usually has four main parts:

  1. API endpoint - the URI to which the request is made.
  2. HTTP method - the most commonly used methods are GET, POST, PUT, PATCH, and DELETE.
  3. HTTP headers - contain metadata about the request, such as the type and size of the resource in the body and user agent name.
  4. Message body - contains data that the client sends to the server, and the server returns to the client.

Supported protocols

This is the list of the most popular protocols supported by Curl. For the complete list, please refer to the Curl documentation.

Protocol Description
HTTP Curl supports the HTTP (Hypertext Transfer Protocol) protocol from HTTP/0.9 to the widely used HTTP/1.1 and supports experimental HTTP/2 and HTTP/3.
FTP Curl supports FTP (File Transfer Protocol) protocol including the TLS version.
SFTP Curl supports SFTP (Secure File Transfer Protocol) protocol and can transfer files over SSH connections.
IMAP Curl supports IMAP (Internet Message Access Protocol) protocol and can download emails from IMAP servers.
POP3 Curl supports POP3 (Post Office Protocol 3) protocol and can download emails from POP3 servers.
SMTP Curl supports SMTP (Simple Mail Transfer Protocol) protocol and can send emails to SMTP servers.

Curl examples of working with HTTP

Following are some of the Curl examples of commands and operations often performed when dealing with HTTP requests.

Send HTTP GET Request

Curl GET request returns response body from specified URL. It can be an HTML page, JSON string, PDF file, or any other data type.

curl https://reqbin.com/echo

To save the received response to a file, use the -o command line parameter..

curl https://reqbin.com/echo -o page.html

Get Response Headers

By default, response headers are hidden in Curl output, to display them use the -i command-line switch.

curl -i https://reqbin.com/echo

Get Only Response Headers

Using the -I command-line switch, you tell Curl to send a HEAD request, which will only receive HTTP headers without a response body.

curl -I https://reqbin.com/echo

Send HTTP POST Request

The -X option allows you to change the HTTP request method. For example, the -X POST parameter will make a POST request.

Curl -X POST https://reqbin.com/echo -d "your data here"

Post JSON

To send JSON to the server, you need to explicitly set the Content-Type header using the -H switch and pass the JSON with the -d command-line option.

curl -X POST https://reqbin.com/echo/post/json
   -H "Content-Type: application/json" 
   -d "{\"login\":\"my_login\",\"password\":\"my_password\"}"

You can also send a JSON file from your local disk by specifying the -d parameter followed by @data.json, where data.json is the JSON file on disk.

curl -X POST https://reqbin.com/echo/post/json -d @data.json

Send User Credentials

If the target URL requires basic HTTP authentication, you can use the -u option to pass the login: password to the server.

curl -u login:password https://reqbin.com/echo

Change User Agent String

The user agent string tells the server the name of the HTTP client that made the request. For example, Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv: 89.0) Firefox/89.0 tells the server that the request is coming from the Firefox web browser. By default, Curl sends the user agent string in Curl/version format (for example, Curl/7.54.0). However, you can specify own user agent using the --user-agent switch.

Curl --user-agent "my-user-agent string" https://reqbin.com/echo

Verbose Output

The --verbose option switches Curl to output detailed logs when sending and receiving data, which can help debug complex situations.

Curl --verbose -I https://reqbin.com/echo

Conclusion

Curl is a potent and handy command-line tool for testing APIs and performing all kinds of URL manipulation tasks such as sending and receiving data. When it comes to debugging network requests, Curl is one of the best tools you can find. Curl is free, adaptable, and works on Linux, Mac, Windows.

See also

Updated: Viewed: 5589 times