• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Curl Follow Redirect(s)

Thumbnail for Razvan LudosanuRazvan Ludosanu

Razvan Ludosanu

Founder, learnbackend.dev

Published: 8/3/2023

About Terminus

The short answer

To make curl follow a redirect, you can use the -L flag (short for --location) as follows:

 $ curl -L <url>

Run in Warp

When sending an HTTP request with curl, the server may respond with an HTTP 3XX (e.g. 300, 301, 302), indicating that the requested resource has been moved to a different location (i.e. URL). This new location can be found in the Location header of the response.

By default, curl will not follow this redirect.

To make curl follow a redirect and get the requested resource from the new URL, you can use the -L flag as shown above.

This flag is often used by developers to get detailed information about how the server processed their request. For example, when performing basic authentication or sending JSON data.

Easily retrieve this command using Warp’s AI Command Search

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

Thumbnail for

Entering curl follow redirect in the AI Command Search will prompt a curl command that can then quickly be inserted into your shell by doing CMD+ENTER.

Following multiple redirects with curl

When using -L, curl will follow up to 50 sequential redirects by default.

Extend the maximum allowed curl redirects

A poorly configured server has the potential to cause curl to get stuck in an infinite loop by sending back a response that redirects to the same URL. To avoid this, you can use the --max-redirs flag to limit the number of redirects curl follows:

 $ curl -L --max-redirs 5 <url>

Run in Warp

If you want to also print the redirects, you can do so by logging the request and response headers while in the process of being redirected. Combine curl with the -v flag (short for --verbose) as follows:

 $ curl -v -L <url>

Run in Warp

Note that when curl follows an HTTP 301, 302, or 303 redirect, and the original request method is not a plain GET (e.g. it is a POST or PUT), the ensuing requests will automatically be converted into a GET, which means that the original body of the request will not be passed along.

curl and 301, 302, etc. redirects

In the context of following redirects, curl treats all redirect requests the same regardless of 3XX status code.

An HTTP 301 redirect indicates that the requested resource has been permanently moved to the URL specified in the Location header of the response. This redirect is often used when a website or API is redesigned or restructured, and some of its URLs have changed.

An HTTP 302 redirect, on the other hand, indicates that the requested resource has been temporarily moved to the URL specified in the Location header of the response. This redirect is often used when a website or API is undergoing short-term changes, such as maintenance operations.

Despite these differences, curl invoked with -L will simply carry on to make the ensuing request.

Avoiding common pitfalls

Verifying a broken redirect chain

curl may sometimes fail to follow a redirect sent by a server due to a broken or invalid URL. To verify that the redirect target is valid and accessible, you can use the -I (capital “i”) flag to only fetch the HTTP headers without including the content.

 $ curl -I <url>

Run in Warp

Forwarding credentials to another host

When performing an authentication with curl, the provided credentials will not be forwarded alongside the request if a redirect takes curl to a different host than the initial one. To bypass this behavior, you can use the --location-trusted flag as follows:

 $ curl --location-trusted -u user:password <url>

Run in Warp

Note that you should be particularly careful when using this flag as it may introduce a security breach if the redirect forwards your credentials to an untrusted or malicious host.

Written by

Thumbnail for Razvan LudosanuRazvan Ludosanu

Razvan Ludosanu

Founder, learnbackend.dev

Filed Under

Related Articles

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

UnixLinux
Thumbnail for Razvan LudosanuRazvan Ludosanu

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

LinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

LinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

Linux Chmod Command

Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.

Linux
Thumbnail for Razvan LudosanuRazvan Ludosanu

POST JSON Data With Curl

How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.

BashUnixLinux
Thumbnail for Neeran GulNeeran Gul

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Linux

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

Linux

Switch Users In Linux

Learn how to switch between users, log in as another user, and execute commands as another user in Linux.

Linux

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.

Linux

Delete Files In Linux

Learn how to selectively delete files in Linux based on patterns and properties using the rm command.

Linux

Find Files In Linux

Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.

Linux

Copy Files In Linux

Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.

Linux

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Thumbnail for null