无法在Python请求中获取'set-cookie'标头

问题描述 投票:0回答:1
# python version: Python 3.11.4
# requests library version: 2.31.0

import requests
import json

# In the actual code I put the real domain instead of example.com
url = "http://api.example.com/auth/sign_in"

headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "en-US,en;q=0.7",
    "Connection": "keep-alive",
    "Content-Type": "application/json",
    "Host": "api.example.com",
    "Origin": "http://www.example.com",
    "Referer": "http://www.example.com/",
    "Sec-Gpc": "1",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
}

payload = {
    "user_login_id": "admin",
    "user_login_pw": "123123"
}

# At first I tried calling the API with `requests.post(url, headers=headers, data=json.dumps(payload))`
# instead of `session = requests.Session()` and session.post(...)`, but the result was the same.

session = requests.Session()

response = session.post(url, headers=headers, data=json.dumps(payload))

if response.status_code == 200:
    data = json.dumps(dict(response.headers), indent=4)
    print(f"response headers: {data}")
    set_cookie = response.headers.get('set-cookie')
    if set_cookie:
        print(f"Set-Cookie Header: {set_cookie}")
    else:
        print("Set-Cookie Header not found in response.")
else:
    print(f"API request failed. status code: {response.status_code}")

当我运行此代码时,这会输出到控制台:

response headers: {
    "x-powered-by": "Express",
    "access-control-allow-origin": "http://www.example.com",
    "vary": "Origin",
    "access-control-allow-credentials": "true",
    "content-type": "application/json; charset=utf-8",
    "content-length": "26",
    "etag": "W/\"1a-pIPrt4esgEyEkX/w62Rnrj9XXdg\"",
    "date": "Sat, 09 Sep 2023 06:33:34 GMT",
    "connection": "close"
}
Set-Cookie Header not found in response.

但是,对相同 API 的调用在浏览器的开发者工具中会出现不同的情况。在网络选项卡中,该 API 的响应标头显示如下。

HTTP/1.1 302 Found
x-powered-by: Express
access-control-allow-origin: http://www.example.com
vary: Origin, Accept
access-control-allow-credentials: true
location: /auth/sign_in_success
content-type: text/plain; charset=utf-8
content-length: 43
set-cookie: connect.sid=s%3Aen3W3Eq0yQs_k7rJZUgWImUAIJKCJNh1.vNS6oiSysS408wodfB%2FBv64IBt7qCaRAfviZw8LQ3Kc; Path=/; HttpOnly
date: Sat, 09 Sep 2023 04:42:01 GMT
connection: close

我需要获取有关

set-cookie
的信息,但我在 python 代码输出中看不到该信息。我怎样才能实现这个目标?

搜索后,我看到另一个stackoverflow问题的答案说使用

session
代替
requests.post()
,所以我使用了
session.post()
,但我无法获得有关
set-cookie
的任何信息。

python api cookies python-requests http-headers
1个回答
0
投票

抱歉。这是我愚蠢的错误。

我只需更改代码即可成功获得它:

# python version: Python 3.11.4
# requests library version: 2.31.0

import requests
import json

url = "http://api.example.com/auth/sign_in"

headers = { same headers value as previous code... }

payload = { same payload value as previous code... }

session = requests.Session()

response = session.post(url, headers=headers, data=json.dumps(payload))

# just using `session.cookies.get_dict()`
print(f"Set-Cookie Header: {session.cookies.get_dict()}")
© www.soinside.com 2019 - 2024. All rights reserved.