I want to use python request to login to a Laravel app and get the content of the first page after login, I tried:
import requests
import re
URL = 'laravelapp.url'
session = requests.session()
front = session.get(URL)
csrf_token = re.findall(r'<input type="hidden" name="_token" value="(.*)"', front.text)[0]
print(csrf_token)
print(session.cookies['XSRF-TOKEN'])
payload = {
'email': '[email protected]',
'password': 'testtest',
'_token': csrf_token,
}
r = requests.post(URL + '/login', data=payload)
print(r)
But this unfortunately returns only a 419 Error. So there seems to be anything wrong with csrf token? But I cant understand what is going wrong, the cookies should be managed by .sessions()
and I extracted the csrf token from the login form and put it as param to the post data. So, what is missing?
You should send cookies along with your request, as in:
import requests
import re
URL = 'laravelapp.url'
session = requests.session()
front = session.get(URL)
csrf_token = re.findall(r'<input type="hidden" name="_token" value="(.*)"',
front.text)[0]
cookies = session.cookies
payload = {
'email': '[email protected]',
'password': 'testtest',
'_token': csrf_token,
}
r = requests.post(URL + '/login', data=payload, cookies=cookies)
print(r.text)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With