Ad
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?
Ad
Answer
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)
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad