curl Command Cheatsheet

View Categories

curl Command Cheatsheet

3 min read

Guide to the curl Command in Linux #

The curl command is a versatile tool in Linux used for transferring data to or from a server. It supports various protocols, including HTTP, HTTPS, FTP, and others, and is particularly useful for API testing and downloading files.

Basic Syntax of the curl Command #

The command syntax is as follows:

curl [OPTIONS] URL

  • OPTIONS: Flags to modify the command’s behavior, such as setting headers, specifying request methods, or saving output to a file.
  • URL: The web address to which curl will send a request or from which it will retrieve data.

Option Table for the curl Command #

Option Description
-o FILE Saves the output to a specified file (e.g., curl -o page.html http://example.com saves the content to page.html).
-O Uses the remote file name and saves it locally (e.g., curl -O http://example.com/image.jpg saves the file as image.jpg).
-L Follows redirects (useful for URLs that redirect to other locations).
-I Fetches only the HTTP headers without the body (useful for getting metadata).
-X METHOD Specifies the HTTP method to use, such as GET, POST, DELETE (e.g., curl -X POST http://example.com).
-H "Header: Value" Adds a custom header to the request (e.g., curl -H "Authorization: Bearer token" http://example.com).
-d "data" Sends data with the request (commonly used with POST requests).
-u USER:PASS Sets basic authentication (e.g., curl -u admin:password http://example.com).

Examples of Using the curl Command #

Here are some common examples of the curl command in use:

Download a File #

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

This command downloads file.zip from http://example.com and saves it with the same name.

Save Output to a File #

curl -o mypage.html http://example.com

Saves the output from http://example.com to a local file named mypage.html.

Follow Redirects #

curl -L http://example.com

Follows any redirects from the original URL to the final destination.

Retrieve HTTP Headers Only #

curl -I http://example.com

Fetches and displays only the HTTP headers from the URL, omitting the response body.

Send Data with POST Request #

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

Sends data in the body of a POST request to http://example.com/api.

Add Custom Headers #

curl -H "Content-Type: application/json" -H "Authorization: Bearer token" http://example.com

Sets custom headers in the request, useful for specifying content types or authorization tokens.

Interpreting curl Output #

When using curl, the output generally includes the following:

  • Response Body: The content of the requested page or data, unless options suppress or redirect it.
  • Headers: HTTP headers are included if requested with -I or when troubleshooting HTTP status codes.
  • Progress Meter: Displays the download or upload progress if the data transfer is large.

Summary #

The curl command is an essential Linux tool for web and API interactions, supporting numerous options for managing headers, data payloads, request methods, and more. It’s a powerful utility for downloading files, testing APIs, and checking server responses.

SHARE ON:

Powered by BetterDocs