1. Requests介绍及简单使用
- 介绍
- 流行的接口http(s)请求工具
- 使用功能强大、简单方便、容易上手
- 安装
- pip3 install requests
- 简单使用
import requests
requests.get("http://www.baidu.com")
- Requests请求返回介绍
r.status_code #响应状态
r.content #字节方式的响应体,会自动为你解码 gzip 和 deflate 压缩
r.headers #以字典对象存储服务器响应头,若键不存在则返回None
r.json() #Requests中内置的JSON
r.url # 获取url
r.encoding # 编码格式
r.cookies # 获取cookie
r.raw #返回原始响应体
r.text #字符串方式的响应体,会自动根据响应头部的字符编码进行
r.raise_for_status() #失败请求(非200响应)抛出异常
2. Requests用例编写
- 编写登录用例脚本
url = "http://127.0.0.1:8080/authorizations/"
data = {"username":"test123",
"password":"12345678"}
r= requests.post(url,json=data)
- 编写个人信息获取脚本
url = "http://127.0.0.1:8080/user/"
headers = '{"Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NjUwNjMwOTksInVzZXJfaWQiOjEsImVtYWlsIjoiOTUyNjczNjM4QHFxLmNvbSIsInVzZXJuYW1lIjoicHl0aG9uIn0.f3GdeSnzb3UGs-w1p1ejZ1rNLaaiBOAHUnN8_pq8LDE"}'
r = requests.get(url,headers=headers)
- 商品列表数据
url = "http://127.0.0.1:8080/categories/115/skus"
data = {
# "categroy_id":"115",
"page":"1",
"page_size": "10",
"ordering": "create_time",#'create_time', 'price', 'sales'
}
r = requests.get(url, json=data)
- 添加到购物车
def cart():
url = "http://127.0.0.1:8080/cart/"
data = {
"sku_id": "3",
"count": "1",
"selected": "true",
# "selected":True
}
headers = {"Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NjU4NTU2NzcsInVzZXJfaWQiOjEsImVtYWlsIjoiOTUyNjczNjM4QHFxLmNvbSIsInVzZXJuYW1lIjoicHl0aG9uIn0.yotHV8wMX7MKHyioFDaGnrjTDTAYsLB0R8Qsungw_ms"}
r = requests.post(url, json=data,headers=headers)
- 保存订单
def orders():
url = "http://127.0.0.1:8080/orders/"
data = {
"address": "1",
"pay_method": "1",
}
headers = {"Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NjU4NTU2NzcsInVzZXJfaWQiOjEsImVtYWlsIjoiOTUyNjczNjM4QHFxLmNvbSIsInVzZXJuYW1lIjoicHl0aG9uIn0.yotHV8wMX7MKHyioFDaGnrjTDTAYsLB0R8Qsungw_ms"}
r = requests.post(url, json=data,headers=headers)
3. Requests方法封装
- 封装Get方法
#1、创建封装get方法
def requests_get(url,headers):
#2、发送requests get请求
r = requests.get(url,headers = headers)
#3、获取结果相应内容
code = r.status_code
try:
body = r.json()
except Exception as e:
body = r.text
#4、内容存到字典
res = dict()
res["code"] = code
res["body"] = body
#5、字典返回
return res
- 封装Post方法
#post方法封装
#1、创建post方法
def requests_post(url,json=None,headers=None):
#2、发送post请求
r= requests.post(url,json=json,headers=headers)
#3、获取结果内容
code = r.status_code
try:
body = r.json()
except Exception as e:
body = r.text
#4、内容存到字典
res = dict()
res["code"] = code
res["body"] = body
#5、字典返回
return res
- 重构GET和POST方法
- 定义公共方法
- 增加cookies,headers参数
- 根据参数method判断get/post请求
- 定义公共方法
def requests_api(self,url,data = None,json=None,headers=None,cookies=None,method="get"):
if method =="get":
#get请求
self.log.debug("发送get请求")
r = requests.get(url, data = data, json=json, headers=headers,cookies=cookies)
elif method == "post":
#post请求
self.log.debug("发送post请求")
r = requests.post(url,data = data, json=json, headers=headers,cookies=cookies)
#2. 重复的内容,复制进来
#获取结果内容
code = r.status_code
try:
body = r.json()
except Exception as e:
body = r.text
#内容存到字典
res = dict()
res["code"] = code
res["body"] = body
#字典返回
return res
- 重构Get方法
#1、定义方法
def get(self,url,**kwargs):
#2、定义参数
#url,json,headers,cookies,method
#3、调用公共方法
return self.requests_api(url,method="get",**kwargs)
- 重构Post方法
def post(self,url,**kwargs):
#2、定义参数
#url,json,headers,cookies,method
#3、调用公共方法
return self.requests_api(url,method="post",**kwargs)