Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

How to Use curl for HTTP Requests in Linux

Published

Linux

If you’ve spent any time working with Linux, you’ve likely crossed paths with curl. Maybe you used it to quickly check if a website is up, to grab a file from the internet, or to test an API endpoint. Whatever your use case, curl is one of those tools that, once mastered, becomes a Swiss Army knife for anyone who lives at the command line.

But what exactly is curl? And how do you use it beyond the basics? This guide will walk you through everything you need to know about using curl for HTTP requests in Linux. We’ll start with the basics, explore common (and uncommon) options, dive into practical, real-world scenarios, and finish with scripting tips and troubleshooting. By the end, you’ll have all the knowledge you need to use curl like a pro—and to solve web, API, and file-transfer problems from your terminal with confidence.

What Is curl, Really?

Let’s start with the basics. curl stands for “Client URL.” It’s a command-line utility designed to transfer data to or from a server using a variety of protocols—HTTP and HTTPS being the most common for most users, but it also supports FTP, SFTP, SCP, and many more.

If you want to:

  • Check if a website is up,
  • Download a file or webpage,
  • Send (or test) data to an API,
  • Upload data,
  • Work with cookies, authentication, or headers,
  • Automate web requests in scripts,

then curl is your tool. It’s lightweight, powerful, and usually already on your system.

Why Do People Love curl?

  • Instant feedback: Test a web service or API in seconds.
  • Flexible and scriptable: Automate tasks, schedule downloads, or integrate into your own scripts.
  • Not just for downloading: Send POST requests, handle cookies, authenticate with APIs, and much more.
  • Universal: Available on almost every Linux distro (and on macOS and Windows too!).

Installing curl on Linux

Most Linux distributions come with curl pre-installed. To check if it’s available, open your terminal and type:

curl --version

If you see version information, you’re good to go. If not, you can install it with your package manager.

For Debian/Ubuntu:

sudo apt update
sudo apt install curl

For Fedora:

sudo dnf install curl

For Arch Linux:

sudo pacman -S curl

After installation, check the version again:

curl --version

Basic Syntax

At its most basic, curl works like this:

curl [options] [URL]

If you just type a URL, curl fetches the page and prints the raw HTML to your terminal.

Fetching Web Pages

Step 1: To retrieve the contents of a web page:

curl https://example.com

Step 2: If you want to save it to a file:

curl -o output.html https://example.com

Step 3: Want to save a file using its original name from the server? Use a capital -O:

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

This is handy for downloading images, PDFs, or any file you want to keep.

Making HTTP Requests

GET Requests

curl https://jsonplaceholder.typicode.com/posts/1

POST Requests

curl -d "username=alice&password=1234" -X POST https://httpbin.org/post

POST JSON Data

curl -X POST -H "Content-Type: application/json" \
    -d '{"username":"alice","password":"1234"}' \
    https://httpbin.org/post

You can use any HTTP method with -X:

curl -X PUT -d "field=value" https://httpbin.org/put
curl -X PATCH -d "data=patch" https://httpbin.org/patch
curl -X DELETE https://httpbin.org/delete

Sending HTTP Headers

To add custom headers (for example, authentication tokens):

curl -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/data

Add multiple headers by repeating -H.

Viewing HTTP Response Headers

Just want to see headers (like status code, content type)?

curl -I https://example.com

To see both headers and body together:

curl -i https://example.com

Verbose output (see the entire HTTP request and response):

curl -v https://example.com

Following Redirects

Some URLs will redirect you to another page or site. By default, curl does NOT follow redirects. To change this:

curl -L https://bit.ly/someurl

-L tells curl to automatically follow HTTP redirects (3xx status codes).

Downloading and Uploading Files

Downloading

curl -O https://example.com/image.png

Resume Download

curl -C - -O https://example.com/largefile.zip

Uploading

curl -F "file=@/path/to/file.txt" https://httpbin.org/post

This simulates a form upload, just like a browser.

Using Cookies and Sessions

Many websites use cookies to track sessions. To save cookies to a file:

curl -c cookies.txt https://example.com/login

To send cookies from a file on later requests:

curl -b cookies.txt https://example.com/profile

This can be useful for automating logins or scraping data from authenticated sites.

Authentication with curl

Basic Authentication

curl -u username:password https://example.com/protected

Bearer Token Authentication

curl -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/secure

Handling Query Parameters

curl "https://example.com/search?q=linux+curl"

Always use quotes if your URL contains special characters or spaces.

Working with APIs and JSON Responses

Most modern APIs return JSON. You can make the output more readable with the jq tool:

sudo apt install jq
curl -s https://api.github.com/users/octocat | jq .

Downloading Multiple Files

You can download several files in a single command:

curl -O https://example.com/file1.txt -O https://example.com/file2.txt

Or, download a list of URLs from a file:

while read url; do curl -O "$url"; done < urls.txt

Controlling Bandwidth and Timeouts

To limit download speed:

curl --limit-rate 500K -O https://example.com/largefile.iso

To set a maximum time for the request:

curl --max-time 20 https://example.com

Setting User-Agent and Other Options

Websites sometimes expect a browser-like User-Agent string:

curl -A "Mozilla/5.0 (compatible; MyBot/1.0)" https://example.com

You can change the request method with -X, but for most use cases, specifying -d (for POST) or -F (for form) is enough.

Monitoring Site Status and HTTP Codes

To check if a website is up by fetching just the HTTP status code:

curl -s -o /dev/null -w "%{http_code}" https://example.com

To see only the HTTP status header:

curl -Is https://example.com | head -n 1

Scripting and Automation with curl

Because curl works at the command line, it’s easy to use in bash scripts for monitoring, downloading, or API automation.

Website Monitor Script

#!/bin/bash
URL="https://example.com"
STATUS=$(curl -s -o /dev/null -w "%{http_code}" $URL)
if [ "$STATUS" -eq 200 ]; then
   echo "Site is up."
else
   echo "Site returned status: $STATUS"
fi

Fetch API Data Automatically

#!/bin/bash
curl -s https://api.coindesk.com/v1/bpi/currentprice.json -o bitcoin_price.json

You can schedule these with cron for regular checks.

Troubleshooting curl Issues

Certificate errors

curl -k https://self-signed.badssl.com/

Timeouts

curl --max-time 5 https://slow.example.com

Authentication errors

Double-check your credentials, tokens, and the exact header format.

Malformed URL

If you get an error about a malformed URL, double-check for typos, missing quotes, or special characters.

Unexpected output

curl -v https://example.com

curl vs wget: What’s the Difference?

curl: Extremely flexible, supports many protocols, great for APIs, HTTP headers, cookies, POST requests, and scripting.

wget: Simpler syntax, excels at recursive downloads and mirroring entire sites or directories.

For most HTTP API or web testing tasks, curl is the better fit.

Best Practices When Using curl

  • Always use HTTPS for sensitive data to protect your credentials and privacy.
  • Quote your URLs and data to avoid issues with special characters.
  • Save frequent commands in scripts for repeatability and automation.
  • Use -L if you expect redirects (many short links or download sites do this).
  • Never share credentials or tokens in scripts posted publicly.
  • Combine curl with jq, awk, or grep for powerful, on-the-fly data processing.
  • Check exit codes for error handling in scripts.

Frequently Asked Questions

Is curl installed by default on Linux?
Usually yes, but you can easily install it with your package manager.

Can curl upload files?
Yes, use -F for form uploads or -T for direct transfers.

How can I view the full HTTP request and response?
Use the -v flag.

How do I send JSON data?
Use -H “Content-Type: application/json” with -d.

Can I use curl in cron jobs or scripts?
Absolutely! curl is built for automation.

Conclusion

curl is one of the most useful and versatile tools in any Linux user’s arsenal. Whether you’re checking a website, testing an API, downloading a file, or automating a workflow, curl has you covered. The more you experiment, the more you’ll discover just how much you can do from the command line—with just a single, powerful tool.

Don’t be afraid to experiment, read the man page, and incorporate curl into your daily workflow. The time you invest will pay you back again and again, every time you need to interact with the web from your Linux machine.

Related guides

Account services are unavailable until the production backend is configured.