How to Follow Redirects in cURL: A Comprehensive Guide

How to Follow Redirects in cURL: A Comprehensive Guide

Today, we’re diving into cURL, a nifty command-line tool for talking to servers and grabbing data. Sometimes, the servers we chat with send us on a little detour with HTTP redirects like 301 or 302. Here’s the deal: cURL doesn’t play follow-the-leader by default. You gotta show it the way.

Ready to learn how to make cURL dance to your redirect tunes? Great! But before we start, make sure you’ve got cURL cozy on your computer. If not, no worries, just grab it from here: cURL Download and make sure it’s all set up in your system’s PATH. Let’s get rolling!

The “-L” or “–location” Option

The simplest way to instruct cURL to follow redirects is by using the “-L” or “–location” option. This option tells cURL to follow any HTTP or HTTPS redirects encountered during the request.


curl -L

In the above command, cURL will automatically follow any redirects encountered while requesting

Controlling the Maximum Number of Redirects

By default, cURL follows up to 50 redirects before giving up. However, you can adjust this limit using the “-L” option followed by the maximum number of redirects you want to allow.


curl -L --max-redirs 10

In the above command, cURL will follow a maximum of 10 redirects while requesting

Verbose Mode for Redirect Details

If you want to see the details of the redirects followed by cURL, you can enable the verbose mode using the “-v” or “–verbose” option.


curl -L -v

With the “-v” option, cURL will display verbose output, including information about the redirects it follows.

Handling Redirects with Different HTTP Methods

By default, when cURL encounters a redirect, it will perform a GET request to the new location. However, you might need to follow redirects while preserving the original HTTP method used for the initial request.

To accomplish this, you can use the “-X” or “–request” option along with the HTTP method you wish to use. Combine it with the “-L” option to follow redirects.


curl -L -X POST

In the above command, cURL will issue a POST request to and follow any subsequent redirects using the same HTTP method.

Handling Redirects with Cookies

If you are dealing with redirects that depend on cookies, you can use the “–cookie” option to provide cookies during the request and subsequent redirects.


curl -L --cookie "session_id=abcd1234"

In the above command, cURL sends the cookie “session_id=abcd1234” with the initial request and any subsequent requests due to redirects.

Wrapping it Up

Okay, let’s sum things up. When you’re dealing with HTTP requests using cURL, following redirects is a big deal. You don’t want to miss out on the good stuff. So, here’s the deal:

  • Use the right options, like “-L” or “–location,” to tell cURL to chase those redirects.
  • You can also set how many redirects it should follow and make sure it keeps your HTTP methods and handles cookies.
  • With all these tricks up your sleeve, you’re all set to handle different situations and grab the data you want from those tricky redirected URLs. Happy curling!