The curl Command: A Practical Guide

Published by The Cyber Staff on

Command-line terminal window on a dark screen

curl is a free command-line tool for sending requests to servers and seeing exactly what comes back. It’s installed by default on Windows 10 and later, macOS and most Linux distributions, which makes it one of the most useful tools for testing websites and APIs, troubleshooting connections and checking security settings, all without opening a browser.

Command-line terminal window on a dark screen
curl works the same way in a terminal on Windows, macOS or Linux. Photo: Gabriel Heinzer / Unsplash

Basic Syntax

curl [options] <URL>

With no options, curl sends a GET request and prints the response body to the screen. Options change the request method, add headers or data, save output to a file, or show more detail about the connection. If you’re new to how requests and responses work, start with our overview of how web requests work.

Most Useful Options

OptionWhat it does
-I, --headFetch only the response headers
-i, --includeShow headers along with the body
-v, --verboseShow the full conversation, including the TLS handshake
-L, --locationFollow redirects
-o / -OSave output to a named file / to the remote file’s name
-X, --requestSet the HTTP method (POST, PUT, DELETE and so on)
-H, --headerAdd a custom request header
-d, --dataSend data in the request body (implies POST)
-u, --userSend basic authentication credentials
-s, --silentHide the progress meter, useful in scripts

Windows tip: In Windows PowerShell, curl may be an alias for a different built-in command. Type curl.exe to make sure you’re running the real curl.

Practical Examples

Check a site’s headers and security settings

curl -I https://www.example.com

The response shows the status code, server details and security headers. It’s a quick way to confirm whether a site sends headers like Strict-Transport-Security or Content-Security-Policy.

Confirm HTTP redirects to HTTPS

curl -IL http://www.example.com

Following the redirects shows each hop, so you can verify that visitors who type http:// are sent to the secure version of your site.

Troubleshoot a connection or certificate

curl -v https://www.example.com -o /dev/null

Verbose mode shows DNS resolution, the TLS version, the server’s certificate and its expiration date, and every header sent and received. The -o /dev/null discards the page body (use -o NUL on Windows).

Download a file

curl -L -O https://www.example.com/files/report.pdf

Send JSON to an API

curl -X POST https://api.example.com/v1/items \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_TOKEN" \
  -d '{"name": "test item"}'

Reading the token from an environment variable ($API_TOKEN) keeps it out of the command itself.

Close-up of a computer screen showing lines of code
curl shows you exactly what a server sends back. Photo: Nick Karvounis / Unsplash

Use curl Safely

  • Don’t disable certificate checks. The -k (--insecure) option skips certificate verification and removes the protection HTTPS provides. Fix the certificate problem instead, and never use -k in scripts that run in production.
  • Keep secrets off the command line. Passwords and tokens typed directly into a command can end up in shell history and process lists. Use environment variables, a credentials file, or let curl prompt for the password by entering only the username with -u.
  • Think twice before piping to a shell. Install instructions that run curl ... | sh execute whatever the server sends. Download the script first, review it, and verify any published checksum before running it.
  • Only test systems you’re authorized to test. Sending requests to someone else’s application to probe for weaknesses without permission can violate laws and terms of service.

The Bottom Line

curl is one of the simplest ways to see how a website or API really behaves: which headers it sends, where it redirects, and what its certificate looks like. A handful of options covers most everyday tasks. If you’d like help reviewing your website’s security configuration, contact us.


0 Comments

Leave a Reply

Avatar placeholder

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