Linux curl Command: Make URL Requests and Inspect HTTP Responses

curl is a tool for requesting or sending data to a URL. It is used for various tasks such as checking HTTP response headers, calling APIs, and downloading files, and by default, it shows the response body in standard output.

What is the curl command?

Even if an HTTP error status is returned, the default exit status of curl can be successful. To treat 4xx and 5xx as failures in scripts, use --fail or, depending on the situation, --fail-with-body. -L follows redirects, and -o saves the result to a specified file.

Basic syntax

curl [options] URL

Installed implementations and options may vary by distribution. Check the description of your current system with man curl.

Examples

Checking response headers

If the server supports HEAD requests, you can see the headers and HTTP status.

curl -I https://example.com/

Checking body and status

Follows redirects while treating HTTP error statuses as failures.

curl -fL https://example.com/

Save to file

Writes the response body to the specified file in the current directory.

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

Result of checking only the HTTP status

curl sends a URL request and checks the response. The following discards the body and only displays the HTTP status code, making it easy to distinguish results such as 200, 404, or 500 in automation.

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

Example output when a normal response is received:

200

200 means that an HTTP response was received, but it is not proof that the page content is as expected. In the case of network errors, the status code may be 000 or an error may be displayed separately. To follow the final destination of redirects, use -L, and be careful about shell history or process list exposure when writing the authentication token directly in the command line.

By default, curl can exit with status 0 after receiving an HTTP 4xx or 5xx response if the transfer itself succeeded. To treat HTTP errors as failures in a script, consider --fail or, when the response body is needed, --fail-with-body. Redact secret tokens before saving verbose output in logs.

Main Options and Format

Options/Format Description
-I Checks the response headers with a HEAD request.
-L Follows redirects.
-o file Saves the response to the specified file.
-O Saves using the remote file name.
-f Treats HTTP 400 and above as errors and suppresses body output.
-H Header Specifies a request header.

Precautions when using

Since the server may refuse HEAD, do not assume the URL does not exist just because curl -I fails; check again with GET. Do not pipe scripts from arbitrary URLs directly into the shell. Putting authentication tokens directly on the command line may leave them in shell history or the process list, and -k lowers security by disabling certificate verification.

Frequently Asked Questions

Why does curl appear to succeed even when it received a 404?

By default, HTTP status and curl's own transfer success are treated separately. In scripts, consider using -f or --fail-with-body.

Official Documentation

For the exact behavior of options and implementation differences, refer to the official curl documentation.

More in This Category
Linux Tutorial / Navigate Directories with pwd, ls, cd, and tree

Linux Tutorial / Navigate Directories with pwd, ls, cd, and tree

Use pwd, ls, cd, and tree to find your current location, inspect directories, navigate paths, and check where each command takes you.

Linux apt-get Command: Run APT Package Operations in Scripts

Linux apt-get Command: Run APT Package Operations in Scripts

Learn how to run APT Package Operations in Scripts with the Linux apt-get command, including practical examples, key options, and important precautions.

Linux nslookup Command: Query DNS Names and Records

Linux nslookup Command: Query DNS Names and Records

Learn how to query DNS Names and Records with the Linux nslookup command, including practical examples, key options, and important precautions.

Linux comm Command: Compare Two Sorted Files

Linux comm Command: Compare Two Sorted Files

Learn how to compare Two Sorted Files with the Linux comm command, including practical examples, key options, and important precautions.

Linux bzip2 Command: Compress a File in bzip2 Format

Linux bzip2 Command: Compress a File in bzip2 Format

Learn how to compress a File in bzip2 Format with the Linux bzip2 command, including practical examples, key options, and important precautions.

Linux groups Command: Show a User's Group Memberships

Linux groups Command: Show a User's Group Memberships

Learn how to show a User's Group Memberships with the Linux groups command, including practical examples, key options, and important precautions.

Linux sync Command: Flush Buffered Writes to Storage

Linux sync Command: Flush Buffered Writes to Storage

Learn how to flush Buffered Writes to Storage with the Linux sync command, including practical examples, key options, and important precautions.

Linux clear Command: Clear the Terminal Display

Linux clear Command: Clear the Terminal Display

Learn how to use the Linux clear command to clear the visible terminal screen and understand scrollback behavior, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux jobs Command: List Jobs in the Current Shell

Linux jobs Command: List Jobs in the Current Shell

Learn how to list Jobs in the Current Shell with the Linux jobs command, including practical examples, key options, and important precautions.

Linux mkdir Command: Create Directories and Parent Paths

Linux mkdir Command: Create Directories and Parent Paths

Learn how to create directories with Linux mkdir, build missing parent paths, set permissions, understand umask behavior, and verify results in scripts.