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.

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.