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 cd Command: Change the Current Directory

Linux cd Command: Change the Current Directory

Learn how to use the Linux cd command to move between directories using absolute, relative, home, and previous paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux chmod Command: Change File and Directory Permissions

Linux chmod Command: Change File and Directory Permissions

Learn how to change File and Directory Permissions with the Linux chmod command, including practical examples, key options, and important precautions.

Linux curl Command: Make URL Requests and Inspect HTTP Responses

Linux curl Command: Make URL Requests and Inspect HTTP Responses

Learn how to make URL Requests and Inspect HTTP Responses with the Linux curl 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 cmp Command: Compare Files Byte by Byte

Linux cmp Command: Compare Files Byte by Byte

Learn how Linux cmp compares two files byte by byte, reports the first mismatch, runs silently in scripts, limits ranges, and differs from diff and checksums.

Linux bunzip2 Command: Decompress bzip2 Files

Linux bunzip2 Command: Decompress bzip2 Files

Learn how to decompress bzip2 Files with the Linux bunzip2 command, including practical examples, key options, and important precautions.

Linux Tutorial / Install and Update Packages with APT and DNF

Linux Tutorial / Install and Update Packages with APT and DNF

Compare APT on Debian or Ubuntu with DNF on Rocky Linux for searching, installing, updating, and removing packages, including repository and transaction checks.

Linux tail Command: View the End of a File and Follow Logs

Linux tail Command: View the End of a File and Follow Logs

Learn how to use the Linux tail command to view the last lines of a file and follow log updates in real time, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux history Command: Review and Reuse Shell Command History

Linux history Command: Review and Reuse Shell Command History

Learn how to use the Linux history command to review, search, reuse, and manage shell command history, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux sort Command: Sort Text Lines

Linux sort Command: Sort Text Lines

Learn how to sort text lines with the Linux sort command, including practical examples, key options, and important precautions.