Curl and Laravel, always redirects

Question

I am trying to do a simple simple curl to a Laravel (5.2) app 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:

The 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

The response header and 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 this really weird since if i use the lib requests from python 3.x:

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)

The response is all html from the page (this is what is supose to happen)

PS: I also used the -L flag with me curl so it can fallow redirects but it appears to end up in an infinite loop (curl: (47) Maximum (50) redirects followed)

Why is this hapening? How to solve it?


Show source
| laravel   | laravel-5   | curl   | python-requests   | cookies   2016-10-05 10:10 1 Answers

Answers to Curl and Laravel, always redirects ( 1 )

  1. 2016-10-05 11:10

    You are setting a cookie, and if that cookie is not present, you redirect the user with a 302 Found, setting the cookie, to the same page. Specifically, you're checking for the lang cookie.

    While Python requests (apparently) retains cookies when redirecting, curl doesn't do this by default. You can enable this by adding a cookie jar (a file where cookies are stored, -c cookies.txt) or setting the cookie manually (-b lang=pt).

    For example:

    $ curl -c cookies.txt -A 'Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0' -LI lemos.migueldvl.com/pt
    HTTP/1.1 301 Moved Permanently
    Date: Wed, 05 Oct 2016 09:18:45 GMT
    Server: Apache
    X-Powered-By: PHP/5.6.26
    Cache-Control: no-cache
    Set-Cookie: XSRF-TOKEN=eyJpdiI6IjJtcHVwUlwvbzJSUlhublVzSCtPVk5BPT0iLCJ2YWx1ZSI6ImlKMEcrQkZDOGlLKzdoUStVUkNLaXBKaDl3N0RvVjBsY1NVMHMrK3hZRCs5ekRzVTg3ZTdUU1J1WHNMb3JUZ1FCWDNrRHRVOGFjdzhsY0JidzQzZEJRPT0iLCJtYWMiOiJjMDhmZmY1OGQxMGY2YWI0YmJkY2M1MzlhYWMyNzNhOTA5YjMwNThjZjM5MTBhOGJhNjJjOWYzZWVlNDY0MGQ0In0%3D; expires=Wed, 05-Oct-2016 11:18:45 GMT; Max-Age=7200; path=/
    Set-Cookie: laravel_session=eyJpdiI6Ik96cjBZRUJnT0daV0lyU3RLYlk2Nmc9PSIsInZhbHVlIjoidW5YMHYybUdMUktLSml0VVwvZWYxSFFTUkVKdEh3WHJQeFlNQmVYZG5aYXNhZyszQWNvY3loN09PWG1TNUJoY0RsMkRFZ0RYZE9XaXRvM1prNzNRKzRnPT0iLCJtYWMiOiI0OTVjYWRiOGIyOGNkZjM1YmFjMDczNTlmMzI4NTI5OWQ3NmZjNDhjZWFlZjQ2MDI3ZWIxZmMzZDVkNzI1ZGQ5In0%3D; expires=Wed, 05-Oct-2016 11:18:45 GMT; Max-Age=7200; path=/; httponly
    Location: http://www.lemos.migueldvl.com/pt
    Cache-Control: max-age=2592000
    Expires: Fri, 04 Nov 2016 09:18:45 GMT
    Vary: Accept-Encoding
    Content-Type: text/html; charset=UTF-8
    
    HTTP/1.1 302 Found
    Date: Wed, 05 Oct 2016 09:18:45 GMT
    Server: Apache
    X-Powered-By: PHP/5.6.26
    Cache-Control: no-cache
    Set-Cookie: lang=pt; expires=Fri, 04-Nov-2016 09:18:46 GMT; Max-Age=2592000; path=/
    Set-Cookie: XSRF-TOKEN=eyJpdiI6InRkMFwvaFpYUENiNjdIbVRPTStuR25BPT0iLCJ2YWx1ZSI6IkZ1V2MwMk41aVYzMWVuNHBEb1JlRks0TCtmNzdxbTJIWGJYNWFldDZZXC93UlRZZmxyK3YxVmJqcDZsQm9pdFhmKzhQa0tUcXY3d0VTR2lQOW1rc1E0Zz09IiwibWFjIjoiZjcyZmI5MGJhNjk0N2Q5ZWQzM2Y4NGExNmIzNmJiNDY1NzI2NzhmMzMzZDJkMGZhMDFkMjRjMmI4M2JiNDMyMCJ9; expires=Wed, 05-Oct-2016 11:18:46 GMT; Max-Age=7200; path=/
    Set-Cookie: laravel_session=eyJpdiI6IjllMDl4cjZPbEpYcWF3VktLOFVCWWc9PSIsInZhbHVlIjoiTFkrMzBMR1ZuV01aMEplMUY5M001T1wvdzZJSWIxVTZDbmdhWVwvRWp4U3pyUUZraUZhZEp1TnR2QzI0TGY0XC9JakZZbnFuNDNtdzc1eTY4c0lYVEpwYkE9PSIsIm1hYyI6Ijc3ODg5NDcyOWIyOGEzYjc4ZmNmYzExYjAyNjY0ZTg1ZmM2YjFjM2FjZDNmODBlN2EyZjJlOTQ3MmZiM2M1YmMifQ%3D%3D; expires=Wed, 05-Oct-2016 11:18:46 GMT; Max-Age=7200; path=/; httponly
    Location: http://www.lemos.migueldvl.com/pt
    Cache-Control: max-age=2592000
    Expires: Fri, 04 Nov 2016 09:18:45 GMT
    Vary: Accept-Encoding
    Content-Type: text/html; charset=UTF-8
    
    HTTP/1.1 200 OK
    Date: Wed, 05 Oct 2016 09:18:46 GMT
    Server: Apache
    X-Powered-By: PHP/5.6.26
    Cache-Control: no-cache
    Set-Cookie: XSRF-TOKEN=eyJpdiI6ImppeXFEaFdWa3NXZlM3cG5iK3Jid0E9PSIsInZhbHVlIjoiT2pGcjFyTnN2ZStIU2tvcW53MmtKdUpudkFIOXNBT1FNQTdLbUlvbVZGbHplYnlzWHJiWW16RXVZRk0rc241Qjcwcm5RVGZTSmdOS0l0cWdDT0x1aUE9PSIsIm1hYyI6IjM0YjhlYzIyMjcwOGUzNzZkOTU5YTk2Yzc4MGI5ODNlMTkyM2QxNjg2NGUzOGVmOTM0NWFlNTNjNWNlZDg3ODMifQ%3D%3D; expires=Wed, 05-Oct-2016 11:18:46 GMT; Max-Age=7200; path=/
    Set-Cookie: laravel_session=eyJpdiI6Ikt4cU5KUm9qNVhKZUxHb2dqOVlDNXc9PSIsInZhbHVlIjoiVlpZdkM5eTYzRFRGcGFaS2lOd3NsYzdsM1Y0MGt5QWlXS25WVzFJZ0k2TzlYZCtUdXZCQTVTNHpoOFNyMjdiV2pNNTFUQ1dMbGt6XC9YR1wvK2FLV2s1QT09IiwibWFjIjoiOTg2ZDMyNzMyODBiODZmMWM2ZTU2NWZlODc3ZmRkNjdiNTA4Yzg2ZDFmZTg0YmJhMzdjMTJiMDVjMWZjNTkwOCJ9; expires=Wed, 05-Oct-2016 11:18:46 GMT; Max-Age=7200; path=/; httponly
    Cache-Control: max-age=2592000
    Expires: Fri, 04 Nov 2016 09:18:46 GMT
    Vary: Accept-Encoding
    Content-Type: text/html; charset=UTF-8
    

Leave a reply to - Curl and Laravel, always redirects

◀ Go back