Whether building and debugging simple requests or complex APIs, curl is an excellent tool in your skillset.
This tutorial will explore the concept of HTTP redirects and how we can use cURL to follow them in a request.
What is an HTTP Redirect?
An HTTP redirect, commonly known as a redirect, refers to a technique where a given address returns a redirection instruction instead of giving back the requested resource or document.
It’s basically what the name suggests. When a client requests a specific resource, instead of the server responding with the queried result, it tells the client to look for the resource in a different location.
The image below shows a basic concept of an HTTP redirect.
Source: Mozilla Developer Network.
The following is a simple HTTP Redirect Header.
HTTP/1.1 301 Moved Permanently
Location: https://linuxhint.com
In the above example, the server tells us that the address http://linuxhint.com has moved permanently to https://linuxhint.com
There are two main types of redirects:
Permanent Redirects
These are the redirects that last and are not valid for that specific request. A permanent redirect tells the client that the requested resource has permanently moved to a new address. A permanent redirect is denoted by HTTP status code 301.
Temporary Redirects
On the other hand, temporary redirects tell the client that the server would like to redirect to a new resource. However, this redirect is not permanent and should not cache this redirect. This means that the redirect will not be there at some later time.
This type of redirect is denoted by the HTTP status code 302.
Now, we have the basics of HTTP redirects let us learn how to tell cURL to follow a redirect response.
cURL Follow Redirect
cURL allows you to follow a redirect by using the -L flag. This flag is derived from the Location Header in a redirect operation.
The command syntax is as shown:
An example of a simple cURL redirect is as shown:
cURL Set Max Redirects
When you allow cURL to follow redirects, it can perform up to 50 redirects. However, you can set a custom value for the redirects using the –max-redirs parameter.
The syntax is as shown:
For example, to set a max of 5 redirects, we can run:
cURL Enable Infinite Redirects
It is no doubt that cURL will attempt to protect you from executing infinite redirects. Hence, if you make a request that returns more than 20 redirects, cURL will automatically cancel the request.
However, you can override this option by setting the –max-redirs parameter to -1
The syntax is as shown:
cURL Prevent HTTP Method Conversion
If you encounter a redirect when making an HTTP POST request, cURL will convert the request to a GET request instead.
However, you can prevent cURL from converting a POST to GET when it encounters a 301, 302, and 303 redirects using the –post[status_code] parameter.
The syntax is shown:
For example, to prevent cURL from converting to GET when it encounters a 301 redirect:
cURL Authenticate Redirect
You may need to provide auth credentials when making a request. However, if cURL encounters a redirect, it will prevent sending the credentials for security reasons.
You can override this by setting the –location-trust parameter.
The syntax is as shown:
Conclusion
Using this tutorial, we discovered how to perform redirection operations using cURL. We covered concepts such as following a simple redirect, setting the number of redirects, infinite redirects, and cURL authentication in a redirect.