requests是一个第三方库,可以使用pip直接安装
构造好url、请求头、请求体,即可进行接口测试,如下是一个简单的demo
import requests from pprint import pprint # 打印json格式 url_1 = 'https://main-store-test.3hea.com/web-platform/1/auth/signIn' # 1.请求URL header_1 = {'Content-Type':'application/json;charset=UTF-8'} # 2.请求头 payload_1 = { # 3.请求体 "loginId": "zhangyang", "password": "dc483e80a7a0bd9ef71d8cf973673924", "verifyId": "3e49b49463ea44e7b46913b36515eeed", "verifyCode": "8844.43", "authType": "usernamePassword", "idType": "USERNAME", "userType": "REGION_USER", "product": "WEBPC" } ret1 = requests.post(url_1, json=payload_1, headers=header_1) # 发送请求 # print(ret1.text) # 返回字符串格式 # print(json.loads(ret1.text)['result']['token']) # 获取token # pprint(ret1.json()) # 返回字典格式 login_token = ret1.json()['result']['token'] # 获取token # print(login_token)
以下是几个调试常用的打印
print(ret1.request.headers) # 打印请求头 print(ret1.request.headers['User-Agent']) # 打印请求头中的某个字段的值 print(ret1.request.body) # 打印请求体 print(ret1.headers) # 打印响应头 print(ret1.url) # 打印请求URL print(ret1.cookies) # 打印cookie print(ret1.status_code) # 打印状态码 print(f'总共耗时{ret1.elapsed.total_seconds()}秒') # 打印接口耗时