Understanding the curl Command

Basic Usage of curl

The curl command is a versatile tool for transferring data to or from a server using various protocols, including HTTP, HTTPS, FTP, and more. It is commonly used for making API requests, downloading files, and testing web services.

curl [OPTIONS] [URL]

Common Options for curl

curl has a rich set of options that can be used to customize its behavior:

Examples of curl Command

Fetch a Webpage

curl https://example.com

This command retrieves the content of the specified URL.

Download a File

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

This command downloads the file and saves it with its original name.

Send Data in a POST Request

curl -d "name=John&age=30" -X POST https://example.com/api

This command sends data to the specified API using a POST request.

Follow Redirects

curl -L https://example.com/redirect

This command follows any redirects until it reaches the final URL.

Send Custom Headers

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

This command includes a custom header in the request for authentication.

Understanding curl Output

The output of the curl command can include:

Summary of Options

Option Description
-V, --version Show version information and exit.
-h, --help Display help information and exit.
-d, --data Send specified data in a POST request.
-X Specify a custom request method to use (e.g., GET, POST).
-H
Pass custom header(s) to the server.
-o Write output to the specified file instead of stdout.
-O Save the file with its original name from the server.
-L, --location Follow redirects.
-u Specify user and password for server authentication.
-i Include HTTP response headers in the output.
-s, --silent Silent mode (no output). Use with -S to show errors.
--limit-rate Limit the download speed to rate.