How to Use the curl Command to Make HTTP Requests in Ubuntu

When working in a headless Ubuntu server environment, you do not have a graphical web browser like Google Chrome or Mozilla Firefox to visit websites or download files. If you need to interact with a web server, download a script, or test a REST API, you must do it entirely from the command line.

The standard tool for this job is the curl command. Short for “Client URL,” curl is a powerful command-line utility that allows you to transfer data to or from a server using various network protocols, most commonly HTTP and HTTPS.

How to Make a Basic HTTP GET Request

The simplest way to use curl is to fetch the HTML source code of a webpage. This is known as a GET request.

  1. Open your Ubuntu terminal.
  2. Type the command followed by the full URL of the website you want to visit:
    curl https://example.com
  3. Press Enter.

The terminal will instantly fill with the raw HTML code of that webpage. While this is difficult for a human to read, it confirms that your server can successfully connect to the internet and communicate with the remote web server.

How to Download Files with curl

You can also use curl to download files, such as a software installation package or a zipped backup file.

If you simply run curl https://example.com/file.zip, the terminal will try to print the raw binary data of the zip file onto your screen, resulting in a mess of unreadable symbols.

To tell curl to save the data as a file instead of printing it to the screen, use the -O (capital O) flag:

curl -O https://example.com/file.zip

This command downloads the file and saves it in your current directory using its original name (in this case, “file.zip”).

If you want to save the downloaded file under a different name, use the lowercase -o flag followed by your chosen filename:

curl -o my_new_name.zip https://example.com/file.zip

How to Inspect HTTP Headers

Sometimes, developers do not care about the actual content of a webpage; they only want to see the server’s HTTP response headers (such as checking if a server is returning a “200 OK” status code or a “404 Not Found” error).

To view only the headers without downloading the full HTML body, use the -I (capital i) flag:

curl -I https://example.com

The output will be a clean list of server metadata, including the content type, date, and server software version.

Following Redirects

If you try to curl a website that has moved to a new URL, the server will respond with a “301 Moved Permanently” header, but curl will not automatically follow the redirect. It will simply stop.

To force curl to automatically follow any redirects and load the final destination page, add the -L flag:

curl -L http://example.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.