首页 文章

在HTTP POST标头中包含会话

提问于
浏览
1

我需要使用Python将JSON上传到API . 我有一个会话标头添加到POST请求以授权请求 . 会话采用以下格式:

会话=值=

我一直试图将它添加为python urllib2库中的 Headers ,但API仍然给我401未经授权 . 到目前为止,我这样做:

req = urllib2.Request(url)
req.add_header('Content-Type', 'application/json')
req.add_header('Set-Cookie', 'session=value=')
response = urllib2.urlopen(req, data=json.dump(data))

我一直在阅读urllib2文档以及http cookies,我找不到通过 Headers 发送此cookie的明确方法 . 请问有谁可以说明如何做到这一点?不幸的是,对于隐私问题,我无法显示网址和Cookie .

提前致谢 .

1 回答

  • 1

    你应该使用 Cookie 而不是 Set-Cokkie . 其他人都很好

    req = urllib2.Request(url)
    req.add_header('Content-Type', 'application/json')
    req.add_header('Cookie', 'session=value=')
    response = urllib2.urlopen(req, data=json.dump(data))
    

    当服务器响应时,它使用 Set-Cookie 在客户端中持久化cookie,当客户端想要回调时,它只使用 Cookie 作为头 .

相关问题