Curl command mastery: A complete guide

how to use curl commands

Curl command was first developed by a developer who wanted to get foreign exchange rates of his local currency everyday. Little did he know, curl command took over the Linux community with its versatile usage. The curl command is used to transfer data to or from a server with the help of different protocols like FTP, HTTPS, and many more. Typical use case of curl command involves testing APIs, downloading files, interacting with web applications, and making web requests. Due to its simplicity, developers and system administrators use curl commands to streamline their workflows, troubleshoot bugs, and automate a lot of tasks. In this article, let us learn about curl commands and how to use curl commands in a wide range of use cases.

How to Install Curl

Before we get to the part where we learn how to use curl, let us see how to install curl in different operating systems.
Execute these commands for UNIX based devices.
Linux: sudo apt-get install curl
macOS: brew install curl

For Windows devices:

  • Go to curl’s official site https://curl.se/download.html.
  • Navigate to the Windows OS section and choose the file appropriate to your device’s architecture (32-bit or 64-bit).
  • Extract the contents of the downloaded file to a path of your choice.
  • Open the Start menu, and go to “Environment Variables”.
  • Select “Edit the system environment variables”.
  • In the System Properties window, click “Environment Variables”.
  • Under “System variables”, find the Path variable and click “Edit”.
  • Click “New” and add the path to the directory where you extracted curl (for example: C:\SampleFolder\curl).
  • Click “OK”.

To check if curl has been installed properly, enter the command curl –version in the command prompt window. If curl is installed properly, the version of curl installed will be displayed.

Basic Elements of the Curl Command

Typically curl commands follow the syntax: curl action URL

In this command, the first element “curl” is the command itself. “Action” is where you instruct the curl command what to do. Some examples are specifying HTTP methods and handling authentication. The last element “URL” is the target URL where we tell from where data is to be fetched from or to where the data has to be sent.

For example, if you execute the command curl http://example.com, the output will be the source HTML file of example.com. Upon executing this command, curl first resolves the URL to an IP address using DNS. Then it establishes a connection to the server hosting the URL. Then curl initiates a HTTP GET request to the server requesting the contents of the page at the URL specified. If the server accepts this request, the server sends the HTML content of the page.

Some Common Options and Flags in Curl

Here are some of the most commonly used options in curl.

To make a simple GET request to a URL: curl http://example.com. This command posts the content of the webpage in the terminal.

To download the content of a URL: curl -o download.html http://example.com. This command saves the content of the webpage as a HTML file named “download.html”.

To download only the HTTP headers and not the entire content: curl -I http://example.com. This command fetches only the server’s response headers like content type and server information.

To download both the HTTP headers and the content: curl -i http://example.com. This command downloads the server response headers and the content.

To initiate a POST request to a URL: curl -X POST -d “field1=value1&field2=value2” http://example.com. This command submits value1 and value2 as respective entries to field1 and field2 parameters.

To authenticate: curl -u username:password http://example.com. This command is used to access resources from a URL that requires authentication.

To fetch the content even after redirects: curl -L http://example.com. This command will follow all the redirect links and provide the content of the final landing URL.

To upload a file to a FTP site: curl -T samplefile.txt http://example.com/upload. This command uploads a file to the specified URL.

To download multiple files from a URL: curl -O http://example.com/image1.png -O http://example.com/image2.png -O http://example.com/image3.png. This command downloads the specified files simultaneously.
To upload a file with authentication: curl -T sampletextfile.txt ftp://ftp.example.com/upload/ –user username:password. This command is used to upload files to FTP servers where authentication is required.

Wrapping Up

Curl commands are crucial for developers and administrators to get their job done easily with the help of automation. Try the commands we have provided and try curl commands in your daily life to make your everyday tasks simpler.

Related Articles

Curl official documentation

Some More Articles that would be of Interest to You