1.介绍:基于
python
语言开发的一个开源的库,能够完全满足基于
HTTP
协议的接口测试。
安装:
安装:
pip install requests
?验证: pip show requests
?2.发送get请求
# 导包
import requests
# 发送请求
response = requests.get("http://www.baidu.com")
# 查看响应 # 查看响应数据编码格式
print("原始的数据编码为:", response.encoding)
print("设置前响应数据:", response.text)
# 设置响应数据编码格式
response.encoding = "utf-8"
print("设置编码后数据编码为:", response.encoding)
print("设置后响应数据:", response.text)
3.发送post请求
response = requests.post(url, data=None, json=None)
""" :param url: 请求的URL
:param data: (可选) 要发送到请求体中的字典、元组、字节或文件对象
:param json: (可选) 要发送到请求体中的JSON数据
"""
data:
参数接收
form
表单数据,后台会?动附加
form
表单请求信息头(
data
数据格式为字典)
json:
参数接收
json
数据,后台会?动附加
json
表单请求信息头
?
(headers = {"Content-Type":"application/json"})
?
4.提交form表单数据
# 导包
import requests
# 发请求
login_url = "http://localhost/index.php?m=Home&c=User&a=do_login"
login_data = {
"username": "13488888888",
"password": "123456",
"verify_code": "8888"
}
response = requests.post(url=login_url, data=login_data)
# 看响应
print(response.json())
?5.提交json数据
?
import requests
#需求: 1. 请求IHRM项目的登录接口,请求数据( {"mobile":"13800000002", "password":"123456"} )
# #2. 登录接口URL:http://ihrm-test.itheima.net/api/sys/login
url_login = "http://ihrm-test.itheima.net/api/sys/login"
jsondata = {
"mobile":"13800000002",
"password":"123456"
}
header = {
"Content-Type":"application/json"
}
response = requests.post(url=url_login,json=jsondata,headers=header)
print(response.encoding)
print(response.json())
""" 1. 请求IHRM项目的登录接口,请求数据( {"mobile":"13800000002", "password":"123456"} )
2. 登录接口URL:http://ihrm-test.itheima.net/api/sys/login
"""
# 导包
import requests
# 发送请求
login_url = "http://ihrm-test.itheima.net/api/sys/login"
login_data = {
"mobile": "13800000002",
"password": "123456"
}
response = requests.post(url=login_url, json=login_data)
# 查看响应
print(response.json())
?6.响应内容解析
用途:断言
实现:
response.status_code
:
响应状态码
response.url
:url
地址信息
response.encoding
:查看响应数据编码格式
response.headers
:查看头部信息
response.cookies
:
查看cookies信息
response.text
:
文本形式查看响应数据
response.content
:字节码形式查看响应数据
response.json()
:
json
形式查看响应数据
7.验证码保存在session
#1. 使用requests库调用TPshop登录功能的相关接口,完成登录操作
# 2. 登录成功后获取‘我的订单’页面的数据
# 接口地址: 获取验证码:http://localhost/index.php?m=Home&c=User&a=verify
# 登录用户:(username: 13088888888, password: 123456, verify_code: 1234)
#登录:http://localhost/index.php?m=Home&c=User&a=do_login
#我的订单接口 :http://localhost/Home/Order/order_list.html
#导包
import requests
#验证码接口地址
verify_Url = "http://localhost/index.php?m=Home&c=User&a=verify"
#登录接口地址
login_Url = "http://localhost/index.php?m=Home&c=User&a=do_login"
#我的订单
order_Url = "http://localhost/Home/Order/order_list.html"
#初始化表单数据
data = {
"username": "13922854653",
"password": "123456",
"verify_code": "8888"
}
#实例化session对象
session = requests.session()
response_verify = session.get(url=verify_Url)
response_verify.encoding = "utf-8"
#<RequestsCookieJar[<Cookie PHPSESSID=vodhgnm9h5l5rgl0vqujhu95k6 for localhost.local/>, <Cookie is_mobile=0 for localhost.local/>]>
print(response_verify.cookies)
response_login = session.post(url=login_Url,data=data)
#{'status': 1, 'msg': '登陆成功', 'result': {'user_id': 2594, 'email': '1260328487@qq.com', 'password': '519475228fe35ad067744465c42a19b2',
# 'paypwd': None, 'sex': 1, 'birthday': 0, 'user_money': '0.00', 'frozen_money': '0.00', 'distribut_money': '0.00',
# 'underling_number': 0, 'pay_points': 100, 'address_id': 0, 'reg_time': 1637734000, 'last_login': 1637734000,
# 'last_ip': '', 'qq': '', 'mobile': '13922854653', 'mobile_validated': 1, 'oauth': '',
# 'openid': None, 'unionid': None, 'head_pic': None, 'province': 0, 'city': 0, 'district': 0,
# 'email_validated': 0, 'nickname': '13922854653', 'level': 1, 'discount': '1.00', 'total_amount': '0.00', 'is_lock': 0,
# 'is_distribut': 1, 'first_leader': 1, 'second_leader': 0, 'third_leader': 0, 'token':
# 'b765f50d83143e89797daa160aa331ce',
# 'message_mask': 63, 'push_id': '0', 'distribut_level': 0, 'level_name': '注册会员'}, 'url': ''}
print(response_login.json())
response_order = session.get(url=order_Url)
response_order.encoding = "utf-8"
print(response_order.text)
?8.用cookie
?
""" 1. 使用requests库调用TPshop登录功能的相关接口,完成登录操作
2. 登录成功后获取‘我的订单’页面的数据
接口地址: 获取验证码:http://localhost/index.php?m=Home&c=User&a=verify
登录用户:(username: 13088888888, password: 123456, verify_code: 1234)
登录:http://localhost/index.php?m=Home&c=User&a=do_login
我的订单:http://localhost/Home/Order/order_list.html """
import requests
# 获取验证码
response = requests.get("http://localhost/index.php?m=Home&c=User&a=verify")
print(response.cookies)
PHPSESSID = response.cookies.get("PHPSESSID")
print(PHPSESSID)
# 登录
login_url = "http://localhost/index.php?m=Home&c=User&a=do_login"
login_data = {
"username": "13488888888",
"password": "123456",
"verify_code": "8888"
}
cookies = {
"PHPSESSID": PHPSESSID
}
response = requests.post(url=login_url, data=login_data, cookies=cookies)
print(response.json())
# 我的订单:http://localhost/Home/Order/order_list.html
response = requests.get("http://localhost/Home/Order/order_list.html", cookies=cookies)
print(response.text)
|