To follow redirect using cURL you need to use the -L option. Here is a sample command that sends a GET
request to our hosted version of
HTTPBin
and follows the redirect:
curl -L https://httpbin.scrapingbee.com/redirect-to?url=https://httpbin.scrapingbee.com/headers?json
Here we ask for cURL to follow redirection, and the url we hit, redirect us to the headers
endpoint. The response will be:
{
"headers": {
"Host": "httpbin.scrapingbee.com",
"User-Agent": "curl/7.86.0",
"Accept": "*/*"
}
}
Now, if we remove the -L option, cURL no longer follow redirection the response will be:
It'll be empty!
What is cURL?
cURL is an open-source command-line tool used to transfer data to and from a server. It is extremely versatile and supports various protocols including HTTP, FTP, SMTP, and many others. It is generally used to test and interact with APIs, download files, and perform various other tasks involving network communication.
What is a redirection?
An HTTP redirect is a way to tell a browser to request a different URL instead of the one originally requested. It is used in cases where a resource has been moved to a different location, or when a different version of the resource is required or preferred. The most common use of redirection is to provide an alternate URL for a web page, so that if the page is moved to a new location, web servers can automatically send visitors to the new location.