获取cookie时返回RequestsCookieJar的解决方法

最近在调用第三方登录接口,需要获取接口的set-cookie

用jmeter可以成功返回set-cookie以及location,在用python时却遇到返回的heasers中没有这两个值,于是输出response.cookie时返回:

废话不说,直接上代码如下:

import requests


class GetToken():
    """获取token"""
    def __init__(self):
        self.url = 'http://192.168.11.xxx:8090/auth/login'
        self.data = {
            'username': 'admin',
            'password': 'admin'
        }
        self.timeout = 10.0
        self.headers = {
            'Connection': 'keep-alive',
            'Content-Length': '29',
            'Content-Type': 'application/x-www-form-urlencoded; charset = UTF - 8',
            'User-Agent': 'Apache-HttpClient/4.5.12(Java / 1.8.0_91)'
        }

    def loginAdmin(self):
        """登录admin获取cookie"""
        session = requests.session()
        session.post(self.url, json=self.data, headers=self.headers, timeout=float(self.timeout))
        cook = session.cookies
        return cook


if __name__ == '__main__':
    gettoken = GetToken()
    return_json = gettoken.loginAdmin()
    # print(return_json)
    print(requests.utils.dict_from_cookiejar(return_json))
    print(requests.utils.dict_from_cookiejar(return_json)['JSESSIONID'])

最后拿到cookie,输入结果如下:

获取cookie时返回RequestsCookieJar的解决方法_第1张图片

你可能感兴趣的:(经验分享,python,cookie,tokenization)