写了个接口测试的脚本,但是执行的时候,如果是session传递,登录没问题。如果是cookie传递的,就会有问题。但是自己把那两个接口单独拎出来顺序执行,
import requests
import os
session = requests.session()
# 第一个url是登录接口,第二个是查询接口
-
url = '*******/login'
-
body = '{"account":"******","password":"***************"}'
-
header_new = {"Host": "www.*****.com","Content-Type": "application/json"}
-
response = session.post(url, data=body, headers=header_new, timeout=60)
-
print(response.text)
-
print(str(response.cookies))
-
-
-
# 获取上面执行结果的Cookie,然后利用CookieJar转为字典:
-
cookie_dict = requests.utils.dict_from_cookiejar(response.cookies)
-
-
# 将字典转为CookieJar:
-
cookies = requests.utils.cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True)
-
-
# 将cookie放到session里进行传递
-
session.cookies = cookies
-
-
-
url = '******/corp/list'
-
body = '{"pageNumber":1,"pageSize":20,"status":2}'
-
header_new = {"Host": "www.*****.com","Content-Type": "application/json"}
-
response = session.post(url, data=body, headers=header_new, timeout=60)
-
print(response.text)
-
print(response.cookies)