遇到的问题:
1、做接口测试时遇到问题,经常碰到开发要求发出完整的请求入参,脚本写的都是拼接前的数据
2、实际调用接口的数据都是加密后的数据,可能是因为加密出现的问题,需要具体的入参来排查
"""
打印完整的请求信息
"""
import requests
def test_r(mothod, url, params=None, body=None, files=None, headers=None, data=None):
if headers is None:
headers = {'Content-Type': 'application/json;charset=utf-8'}
if mothod == "GET":
request = requests.Request(mothod, url=url, params=params).prepare()
elif mothod == "POST":
request = requests.Request(mothod, url=url, params=params, json=body, files=files,
headers=headers, data=data).prepare()
print("--- Request ---")
print(f"URL: {request.url}")
print(f"Method: {request.method}")
print("Headers:")
for key, value in request.headers.items():
print(f" {key}: {value}")
print(f"Body:")
print(request.body)
session = requests.Session()
response = session.send(request)
#
print("--- Response ---")
print(f"Status code: {response.status_code}")
print("Headers:")
for key, value in response.headers.items():
print(f" {key}: {value}")
print("Body:")
print(response.content.decode("utf-8"))
return response.content.decode("utf-8")
这里仅编写了最常见的两种请求方式,可以按照业务场景自行扩展
标签:None,请求,url,打印,request,接口,headers,params,print From: https://www.cnblogs.com/cai11/p/18229224