【问题标题】:Passing a CSRF token into requests.post()将 CSRF 令牌传递到 requests.post()
【发布时间】:2014-10-12 13:49:48
【问题描述】:

我看到了这个帖子 - Passing csrftoken with python Requests

我一直在努力使其适用于 Greenhouse。我正在尝试构建一个自动创建配置文件的脚本。

我可以使用 GET 和 cookie 获取数据,但我想我在使用 X-CSRF 时遇到了困难。我下载了 Mozilla 的 Live HTTP 标头插件来获取 CSRF 令牌,但我不确定如何传递它。

到目前为止我所拥有的:

csrf = 'some_csrf_token'
cookie = 'some_cookie_id'
data = dict('person_first_name'='Morgan') ## this is submitting my name on the form
url = 'https://app.greenhouse.io/people/new?hiring_plan_id=24047'  ##submission form page
headers = {'Cookie':cookie}

r = requests.post(url, data=data, headers=headers)

我应该如何构建我的 requests.post 有什么想法吗?

【问题讨论】:

    标签: python post csrf python-requests


    【解决方案1】:

    如果您希望请求为您处理 cookie,则应设置会话。

    session = requests.session()
    logindata = {'authenticity_token': 'whatevertokenis', 
        'user[email]': 'your@loginemail.com', 
        'user[password]': 'yourpassword',
        'user[remember_me]': '0'}    
    login = session.post('https://app.greenhouse.io/users/sign_in', data=logindata) #this should log in in, i don't have an account there to test.
    data = dict('person_first_name'='Morgan')
    url = 'https://app.greenhouse.io/people/new?hiring_plan_id=24047'
    r = session.post(url, data=data) #unless you need to set a user agent or referrer address you may not need the header to be added. 
    

    【讨论】:

    • @mnjermiah 我仍然卡在登录屏幕上:-(
    • @MorganAllen 嗯,你需要先登录......你应该通过 post 传递authentity_token、user[email]、user[password]、user[remember_me] 到 app.greenhouse.io/users /sign_in 才能进入您想要的页面……您需要登录才能访问该页面。
    猜你喜欢
    • 2014-10-12
    • 2023-04-02
    • 2018-10-23
    • 2015-01-31
    • 2017-04-12
    • 2015-05-13
    • 2017-02-17
    • 2019-06-01
    相关资源
    最近更新 更多