Python3 接口测试,测试需要带登录态的接口 A,requests.cookies 获取到的登录态 cookie 以什么样的语句/格式/语法传到接口 A ?
#方法一:requests.Session
coding = utf-8
import requests,json,urllib
login_url = http://'xxx'
payload = {'postData':'{"userName":"xxx","password":"xxx",\
"validateCode":"","validateSession":"","appId":"","jump":""}'}
s = requests.Session()
req=s.post(login_url,data=payload)
#需要登录态的接口
newurl = 'http://xxx'
req_new = s.post(newurl)
req_new_value = req_new.json()
print(req_new_value)
code = req_new.json()['code']
print(code)
#方法二:手动带上cookies/headers
coding = utf-8
import requests,json,urllib
login_url = http://'xxx'
payload = {'postData':'{"userName":"xxx","password":"xxx",\
"validateCode":"","validateSession":"","appId":"","jump":""}'}
req=requests.post(login_url,data=payload)
headers = req.headers
#把requests.cookies获取到的cookies转化成字典格式
cookies = requests.utils.dict_from_cookiejar(login_cookies)
#需要登录态的接口
newurl = 'http://xxx'
req_new = requests.post(newurl,cookies=cookies)
#headers和cookies都试过,依然提示登录态失效
#req_new = requests.post(newurl,headers=headers,cookies=cookies)
code = req_new.json()['code']
print(code)