Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use python requests to login to laravel app

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?

like image 832
Asara Avatar asked Jan 26 '23 19:01

Asara


1 Answers

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)
like image 163
Amade Avatar answered Jan 31 '23 09:01

Amade