如何发送带有主体,标头和cookie值的urllib请求

时间:2019-09-19 09:53:40

标签: python urllib

早上好, 我已经做到了...我可以发送带有标题和正文信息的请求。但不幸的是,某些URL需要Cookies。如何将硬编码的Cookie放在urllib.request中。

url = "https://example.de"

values = {"username": "TestKunde1","password": "Password1"}
headers = {
    "Content-Type": "application/json",
    "Accept": "application/json",
}

##Cookies = ??????? like something like Cookies = dict(authHint=num; path=/; domain=text; secrue;

body = json.dumps(values).encode("utf-8")
#pprint(data)

try:
    req = urllib.request.Request(url, body, headers)
    with urllib.request.urlopen(req) as f:
        res = f.read()
    pprint(res.decode())
except Exception as e:
    pprint(e)


´´´


0 个答案:

没有答案