Curl Follow Redirect

curl follow redirect

Curl Follow Redirect : In this article, we will talk about the cURL utility. cUrl is a program that can send HTTP requests via the command line, which is useful for many things (like downloading files, publishing data through a Rest API for example…).

Redirection HTTP

HTPP servers often return a redictection for a requested URL such as a 301 or 302 redirect. Here is an example with http://amiradata.com :

curl -i http://amiradata.com
 
HTTP/1.1 301 Moved Permanently
Date: Sat, 18 Apr 2020 15:38:25 GMT
Content-Type: text/html; charset=iso-8859-1
Content-Length: 230
Connection: keep-alive
Location: https://amiradata.com/
Server: test

Redirection is an integral part of the HTTP protocol. It is the server that sends a command back to the client instead of returning the content desired by the client.

Note: the <-i> option is used to specify to print the response headers of the cUrl request

By default, cURL does not follow HTTP redirects. One way around this is to use the -L, –location options.
If this option is set in the cURL query, cUrl will be able to follow up to 50 redirections by default (this limit is to avoid falling into an infinite loop in redirections). You can modify this limit with the –max-redirs option.

Here is an example using the -L option :

curl -iL http://amiradata.com
 
HTTP/1.1 301 Moved Permanently
Date: Sat, 18 Apr 2020 15:47:55 GMT
Content-Type: text/html; charset=iso-8859-1
Content-Length: 230
Connection: keep-alive
Location: https://amiradata.com/
Server: test
HTTP/1.1 200 OK
Date: Sat, 18 Apr 2020 15:47:55 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 83637
Connection: keep-alive
Vary: Accept-Encoding
Last-Modified: Sat, 18 Apr 2020 15:19:29 GMT
Referrer-Policy: no-referrer-when-downgrade
Server: test
Accept-Ranges: bytes

By using this option, we can see that the redirections have been handled correctly and that the data returned by the URL is received.

Pour avoir plus d’informations sur ce sujet, je vous invite à consulter ce lien : https://curl.haxx.se/docs/httpscripting.html

Back to Shell Menu

By ayed_amira

I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)

Leave a comment

Your email address will not be published.