Reputation: 520
I have python code like this
s=requests.Session()
headers = {'User-Agent': 'Mozilla/5.0'}
URL = 'http://test.dev/api/login'
s.get(URL)
csrftoken = s.cookies['csrf']
print csrftoken
login_data = dict(username='test', password='testtest', _token=csrftoken)
ra=s.post(URL, data=login_data, headers = headers)
print ra.request.headers
But I don't want to find the csrftoken = s.cookies['csrf'] from cookie, So how can I fine it with
<input type="hidden" name="csrf">
and is it need to put header Referer=URL when request with csrftoken or it just some site?
Thank you.
Upvotes: 1
Views: 4373
Reputation: 520
Thank you very much Moe Jan.
First I trying you code but it did not get the value.
csrf = soup.find(name="csrf")
So I searching for bs4 and use
csrf = soup.find("input", value=True)["value"]
and it got the value.
My form is like
<form accept-charset="UTF-8" action="http://test.dev/api/login" method="POST">
<input name="_token" type="hidden" value="8z0dMEpVq8jH0VY5zgh8xFhcrQaurz">
</input>
</form>
Upvotes: 0
Reputation: 369
I would try using BeautifulSoup
from bs4 import BeautifulSoup
s=requests.Session()
headers = {'User-Agent': 'Mozilla/5.0'}
URL = 'http://test.dev/api/login'
s.get(URL)
soup = BeautifulSoup(s.get(URL).text)
csrf = soup.find(name="csrf")
This will extract the hidden input that holds the csrf_token
Since I can't see where and how the value of the token is stored this is as far as I can take you.
Upvotes: 4