Curl and Laravel, always redirects

0

I'm trying to make a simple curl to a Laravel app (5.2) running on my machine:

curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1" http://192.168.1.65:8000/pt

With this result:

request header :

*   Trying 192.168.1.65...
* Connected to 192.168.1.65 (192.168.1.65) port 8000 (#0)
> GET /pt/ HTTP/1.1
> Host: 192.168.1.65:8000
> Accept: */*
> User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1

response header / body (header and response body) :

< HTTP/1.1 302 Found
< Host: 192.168.1.65:8000
< Connection: close
< X-Powered-By: PHP/7.0.8-0ubuntu0.16.04.2
< Set-Cookie: lang=pt; expires=Fri, 04-Nov-2016 08:53:50 GMT; Max-Age=2592000; path=/
< Cache-Control: no-cache
< Location: http://192.168.1.65:8000/pt
< Content-Type: text/html; charset=UTF-8
< Date: Wed, 05 Oct 2016 08:53:50 GMT
< Set-Cookie: XSRF-TOKEN=eyJpdiI6IjNReFJiRFpYOG5USEgzaVZ4YWQ5OXc9PSIsInZhbHVlIjoiblBFU0FqRjJ3WFMyajJHZnBlUEMzT2lXK2ZDaGpTVDJnQnZZSXdSNUhTUHQ2QmxjcUZGUDFOUit0NzFKeUxMY28zaUl0VlVBNGtUMUJmYnlxWisrT3c9PSIsIm1hYyI6IjZjZmFlZTcwNGMxOTE1OGM2NjE1ZWM5OWViZjEzMjZmYzIwZTljNWMwYWY1ZmQzZGI3Y2FjZDdiM2Q4Y2IxMmQifQ%3D%3D; expires=Wed, 05-Oct-2016 10:53:50 GMT; Max-Age=7200; path=/
< Set-Cookie: laravel_session=eyJpdiI6IjJ5MTMwYXBpVDlqRTZ6U2NmNjBWb3c9PSIsInZhbHVlIjoiTm10QklTZTAydURkeU1kSm9Eam1UaGg1RlpvQWpncTBJTmRSd2poT01ORVRUa2l3MzNSSjJZTStPMWpGTVdYQ0JFRkt3M2ZUd3NRYVNTS3JLQkpLckE9PSIsIm1hYyI6IjM5MmQ2YzEzNDYwM2M5YTc1MzI0ODZmMjBiYWZiNmYyM2Q4NzE0ZTEyOWE3NWUzZjRjMGIxMGFjMGVjZDgzNGIifQ%3D%3D; expires=Wed, 05-Oct-2016 10:53:50 GMT; Max-Age=7200; path=/; HttpOnly
< 
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="refresh" content="1;url=http://192.168.1.65:8000/pt" />

        <title>Redirecting to http://192.168.1.65:8000/pt</title>
    </head>
    <body>
        Redirecting to <a href="http://192.168.1.65:8000/pt">http://192.168.1.65:8000/pt</a>.
    </body>
* Closing connection 0

I find it very strange, because if you use the python 3.x requests library:

import requests

headers = {'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1'}
req = requests.get('http://192.168.1.65:8000/pt/', headers=headers)
print(req.text)

I'm already returned in response to the entire html page (this is what is supposed to happen)

PS: I've also used the flag -L with my curl to follow the redirects and it gets infinite loop ( curl: (47) Maximum (50) redirects followed )

Why does this happen? How to solve, and get request with curl?

    
asked by anonymous 05.10.2016 / 10:34

1 answer

1

Resolved, it seems that the requests library (even without using Session() ) retains the cookies and places them in the redirect request header, in this case it is the ..../pt cookie that, by the app design, it is always mandatory to exist and by default the PT. The problem is that curl following the redirection did not put the cookie in the header of the request, resulting in an infinite loop of redirects, that is, the correct command so that curl works out:

curl -c cookies.txt -A 'Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0' -Lv http://192.168.1.65:8000/pt

In which I am writing the cookies defined in the response headers in a file to use them also in the next redirect

Or, I can also set the cookie manually, without needing a file:

curl -A 'Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0' -b lang=pt http://192.168.1.65:8000/pt
    
05.10.2016 / 11:31