【问题标题】:Translate cURL headers command to python-requests将 cURL 标头命令转换为 python-requests
【发布时间】:2016-09-30 02:22:26
【问题描述】:

我需要将包含令牌的 POST 传递给网站以获取身份验证密钥,我已经成功地使用 cURL 和以下语句完成了它:

curl -D - --request POST https://login.website.com/g/aaa/authorize --data-urlencode token=[TOKEN]

但是,我似乎无法使用 Python 请求模块获得相同的结果:

auth_token = requests.post('https://login.website.com/g/aaa/authorize', data=token)

auth_token.text 只是给我'Unauthorized'

我尝试将数据 kawrg 传递为:

  • 直接 JSON
  • 一个字符串
  • 在字符串开头添加token=
  • 使用params 代替数据并将其传递给JSON(这让我unexpected argument: {"token" : "value....."}

在这种情况下,-D--data-urlencode(而不仅仅是 --data)的意义是什么,我是否应该尝试将标头作为参数转储到 requests.post() 中?

ЕDIT: 在 cURL 输出中,auth_key 也会在任何 json 开始之前打印出来:

Set-Cookie: auth_key=[auth_key]; Domain=.website.com; expires=Fri, 30-Sep-2016 01:41:09 GMT; httponly; Max-Age=14399; Path=/

【问题讨论】:

    标签: python curl python-requests


    【解决方案1】:

    找到了答案,需要使用requests.json()而不是json.dumps(data),后者格式不正确,所以这段代码运行良好:

    s.token = requests.post(url, data=data)
    s.recieved = s.token.json()
    t = requests.post(url, data=s.recieved)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      相关资源
      最近更新 更多