Skip to content

How to Follow a Redirect with cURL

When a server answers with an HTTP redirect, cURL does not automatically chase it unless you ask.

OUTPUT FORMATSHTMLJSONMarkdown

The -L option tells cURL to follow the redirect chain and return the response from the final destination. This guide shows the flag in a working request, explains the JSON you get back, and clarifies the empty result when -L is missing.

The -L option in one request

Add -L to a cURL command to follow redirects. The example below sends a GET request to a redirect endpoint that points to a headers endpoint. Because -L is present, cURL follows the redirect and returns the headers JSON from the destination.

The command uses the hosted HTTPBin base URL. The redirect-to endpoint receives a url parameter that points to the headers endpoint with json output. cURL follows that redirect automatically.

After running the command, the response is a JSON object containing a headers field. The headers field includes Host, User-Agent, and Accept values. The User-Agent shown is curl/7.86.0, and Accept is */*. These values describe the request that reached the final endpoint.

What happens without -L

Remove -L and cURL stops following the redirect. The redirect response itself is returned, but the destination is not requested. In the captured example, the response body is empty. That empty result is the practical difference: with -L you see the final endpoint's content; without -L you see only the redirect response, which may have no body.

Use this as a quick check: if a cURL request returns an empty body or a 3xx status where you expected JSON, confirm whether -L is present.

Quick decision checklist

  • Use -L when you want the response from the final URL after one or more redirects.
  • Omit -L when you need to inspect the redirect response itself, such as the status code or Location header.
  • If the output is empty and you expected content, check for a missing -L before changing other options.
  • Keep the redirect target URL-encoded when it contains its own query string, as in the example's url parameter.

Field map for the headers response

The JSON response has one top-level field, headers. Inside headers, three fields appear in the captured example:

  • Host: the host that received the final request.
  • User-Agent: the client identifier sent by cURL. The captured value is curl/7.86.0.
  • Accept: the media types the client will accept. The captured value is */*.

This field map helps you confirm that the redirect was followed and that the final endpoint received the request.

Background: cURL and HTTP redirects

cURL is a command-line tool for transferring data to and from servers. It supports HTTP and other protocols and is commonly used to test APIs and inspect network responses.

An HTTP redirect tells the client to request a different URL instead of the original one. Servers use redirects when a resource has moved or when a preferred version of the resource lives at another address. Following the redirect means cURL makes the additional request and returns the destination's response.

Frequently asked questions

Which cURL option follows redirects?

Use -L. It tells cURL to follow the redirect and return the response from the final URL.

Why is my cURL response empty when the URL redirects?

If -L is missing, cURL does not follow the redirect. The redirect response may have no body, so you see an empty result. Add -L to reach the destination.

Does -L change the request method?

The captured example uses a GET request and -L follows the redirect without changing that method. For other methods, check how your server and cURL version handle redirects.

curl -L {{HTTPBIN_BASE_URL}}/redirect-to?url={{HTTPBIN_BASE_URL}}/headers?json
{
  "headers": {
    "Host": "httpbin.scrapingbee.com",
    "User-Agent": "curl/7.86.0",
    "Accept": "*/*"
  }
}