传递CSRF令牌

时间:2014-08-19 17:52:38

标签: python post csrf

这不会超过登录屏幕。我不认为我正确传递了CSRF令牌。我该怎么办?

from bs4 import BeautifulSoup
import requests

url = 'https://app.greenhouse.io/people/new?hiring_plan_id=24047'
cookies = {'_session_id':'my_session_id'}
client = requests.session()

soup = BeautifulSoup(client.get(url, cookies=cookies).content)
csrf_metatags = soup.find_all('meta',attrs={'name':'csrf-token'})[0].get('content')
posting_data = dict(person_first_name='Morgan') ## this is what I want to post to the form
headers = dict(Referer=url, csrf_token=csrf_metatags)
r = client.post(url, data=posting_data, headers=headers)

谢谢!

1 个答案:

答案 0 :(得分:2)

如果您检查代码,您会发现该表单具有隐藏的附加值,如下所示:

<input name="authenticity_token" type="hidden"
value="2auOlN425EcdnmmoXmd5HFCt4PkEOhq0gpjOCzxNKns=" />

您可以使用以下方式捕获此值:

csrf_data = soup.find("input", {"name": "authenticity_token"}).get("value")

现在将值重新附加到发布数据,就像使用person_first_name

一样
posting_data = dict(person_first_name='Morgan',
                    authenticity_token=csrf_data)
相关问题