The Complete Beginner’s Guide to curl

·

If you’ve ever wondered how to interact with websites and APIs directly from your terminal, curl is the tool you need to know. This guide will walk you through everything from what curl is to how to use it effectively.

What is curl?

curl (short for “Client URL”) is a powerful command-line tool that lets you transfer data to or from a server using URLs. Think of it as your terminal’s web browser—but instead of clicking buttons, you type commands.

Why use curl?

  • Test APIs without needing a graphical interface
  • Download files quickly from the command line
  • Automate tasks in scripts
  • Debug web requests to see exactly what’s happening
  • Work with multiple protocols (HTTP, HTTPS, FTP, and more)

Getting Started: Basic curl Commands

1. View Website Content

The simplest curl command fetches and displays webpage content:

curl https://example.com

This retrieves the HTML of the website and displays it in your terminal.

2. Download Files

To save a file from the internet:

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

The -O flag tells curl to save the file with its original name.

Want to give it a different name?

curl -o myfile.txt https://example.com/file.txt

3. Send Data to a Server

When working with APIs, you often need to send data:

curl -X POST -d "name=Sonny" https://api.example.com/submit
  • -X POST specifies the HTTP method (POST, GET, PUT, DELETE, etc.)
  • -d sends data in the request body

4. Work with Authentication

Many APIs require credentials:

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

The -u flag handles basic authentication for you.

Understanding curl -s (Silent Mode)

One of the most useful flags is -s, which enables silent mode.

What happens normally?

When you run curl without any flags, you see a progress meter:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   4891      0 --:--:-- --:--:-- --:--:--  4891

What does -s do?

curl -s https://example.com

With -s (or --silent), curl will:

Hide the progress meter
Suppress error messages
Show only the actual output

This is perfect when you want clean output, especially in scripts.

Pro tip: Silent but show errors

Sometimes you want silent mode but still need to see if something went wrong:

curl -sS https://example.com

The -S flag (capital S) shows errors even in silent mode.

Common curl Flags You Should Know

FlagWhat it doesExample
-OSave file with original namecurl -O https://site.com/file.zip
-oSave file with custom namecurl -o custom.zip https://site.com/file.zip
-sSilent mode (no progress)curl -s https://api.com/data
-SShow errors in silent modecurl -sS https://api.com/data
-XSpecify HTTP methodcurl -X POST https://api.com
-dSend datacurl -d "key=value" https://api.com
-HAdd custom headerscurl -H "Content-Type: application/json"
-uAuthenticationcurl -u user:pass https://site.com
-LFollow redirectscurl -L https://short.link
-IGet headers onlycurl -I https://example.com

Practical Examples

Example 1: Check if a website is up

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

This returns just the HTTP status code (like 200 for success).

Example 2: Download with progress bar

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

The -# shows a simple progress bar instead of the detailed meter.

Example 3: Send JSON data to an API

curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d '{"name":"John","email":"john@example.com"}'

Example 4: Save API response to file

curl -s https://api.github.com/users/octocat > user.json

Example 5: Follow redirects

Some URLs redirect to other pages. Use -L to follow them:

curl -L https://bit.ly/shortened-url

Supported Protocols

curl isn’t just for websites—it supports many protocols:

  • HTTP/HTTPS – Web requests
  • FTP/FTPS – File transfers
  • SMTP – Sending emails
  • IMAP/POP3 – Reading emails
  • SCP/SFTP – Secure file transfers
  • And many more!

Tips for Beginners

  1. Start simple: Begin with basic curl https://example.com commands before adding flags
  2. Use -v for debugging: The -v (verbose) flag shows you everything happening behind the scenes
  3. Combine flags: You can use multiple flags together like curl -sS -L -o file.txt
  4. Check the manual: Run curl --help or man curl for complete documentation
  5. Practice with public APIs: Try https://api.github.com or https://jsonplaceholder.typicode.com for safe testing

Common Use Cases

For Developers

  • Testing REST APIs during development
  • Automating file downloads in deployment scripts
  • Checking server responses and headers
  • Debugging authentication issues

For DevOps/Security

  • Health checks for services
  • Monitoring endpoint availability
  • Testing SSL certificates
  • Scanning for security headers

For Daily Tasks

  • Downloading files from the terminal
  • Checking website status
  • Fetching data for scripts
  • Quick web scraping

Troubleshooting

Problem: Getting SSL certificate errors
Solution: Use -k to skip certificate validation (only for testing!)

curl -k https://self-signed-cert.example.com

Problem: curl command too long
Solution: Use a config file or break it into multiple lines with \

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"data":"value"}' \
  https://api.example.com

Problem: Need to see what’s being sent
Solution: Use verbose mode with -v

curl -v https://example.com

Summary

curl is an essential tool for anyone working with web technologies. Whether you’re:

  • A developer testing APIs
  • A sysadmin monitoring services
  • A beginner learning web protocols
  • Someone who just wants to download files efficiently

…curl has you covered.

Key takeaway: curl -s gives you clean output by hiding progress meters—perfect for scripts and when you just want the data.

Start experimenting with these commands, and you’ll quickly see why curl is installed on virtually every system by default!


Ready to practice? Try running curl -s https://api.github.com/zen for a random piece of GitHub wisdom!

Comments

Leave a Reply

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