requests基本用法
Requests基础语法:
Requests可以在内部封装的函数中设置HTTP接口所需要的请求数据:
import requests
response = requests.post(url="http://www.xxx.com/api/v1/xx", params={"key":"value"},
data={"key":"val"}, json={"key":"val"}, headers={"Content-Type":"application/json"},
cookies=cookie)
response 接口requests.post(...)方法返回的Response对象,该对象包括了服务器返回的响应数据
requests.post(...) 设置请求方法为POST,其他设置方法有:requests.get(),requests.put(),requests.delete()等
url="http://www.xxx.com/api/v1/xx" 设置接口的URL是http://www.xxx.com/api/v1/xx
params={"key":"value"} 设置接口的查询参数为key=value
data={"key":"val"} 设置表单格式请求体数据为key=value
json={"key":"val"} 设置json结构请求体数据为{"key":"val"}
headers={"Content-Type":"application/json"} 设置请求头为{"Content-Type":"application/json"}
cookies=cookie 设置cookies,在这里右边的cookie是一个变量
处理响应数据
打印请求的URL:response.url
打印响应状态码:response.status_code
打印Cookie:response.cookies
打印响应头:response.headers
打印响应正文(响应体)
字节码打印:response.content
文本打印:response.text
Json打印:response.json()
手动管理cookies
import requests
# 获取验证码
response = requests.get(url="http://localhost/index.php?m=Home&c=User&a=verify")
# 提取cookie
cookie = response.cookies
# 登录
response = requests.post(url="http://localhost/index.php?m=Home&c=User&a=do_login",
data="username=13800138006&password=123456&verify_code=8888",
headers={"Content-Type":"application/x-www-form-urlencoded"})
# 打印结果
print(response.text)
session方法
import requests
# 实例化session
session = requests.Sessino()
# 获取验证码
response = session.get(url="http://localhost/index.php?m=Home&c=User&a=verify")
# 登录
response = session.post(url="http://localhost/index.php?m=Home&c=User&a=do_login",
data="username=13800138006&password=123456&verify_code=8888",
headers={"Content-Type":"application/x-www-form-urlencoded"})
# 打印结果
print(response.text)
文件上传方法
# 需求:上传文件(上传文件接口是什么)
# 导包
import requests
# 实例化session对象
session = requests.Session()
# 使用session对象发送验证码接口请求(session发送请求的方式和requests一模一样,区别就是我们是使用
session对象发送请求)
response = session.get(url="http://localhost/index.php?
m=Home&c=User&a=verify&r=0.9698092918823165")
print(response.content)
print(response.cookies)
# 使用session对象发送登录接口请求
response = session.post(url="http://localhost/index.php?
m=Home&c=User&a=do_login&t=0.8916582864091132",
data={"username":"13800138006", "password":"123456",
"verify_code":"8888"},
headers={"Content-Type":"application/x-www-form-urlencoded"})
# 打印登录的结果
print(response.json())
# 上传图片
data = {"id":"WU_FILE_0", "name":"camara.jpg", "type":"image/jpeg",
"lastModifiedDate":"Mon May 31 2021 15:05:08 GMT+0800 (中国标准时间)",
"size":"169267"}
# 要上传的文件
file ={"file":open(r"C:\Users\wind\Pictures\first_requests.jpg", 'rb')}
#调用上传图片接口
response =
session.post(url="http://localhost/index.php/home/Uploadify/imageUp/savepath/head_pic/pic
title/banner/dir/images.html",
data=data,
files=file)
print("上传文件的结果为:", response.json())
用例实现
import unittest, requests
class TestLgShopLogin(unittest.TestCase):
def setUp(self) -> None:
self.session = requests.Session()
def test01_login_success(self):
# 使用session对象发送验证码接口请求(session发送请求的方式和requests一模一样,区别就是我们是使
用session对象发送请求)
response = self.session.get(url="http://localhost/index.php?
m=Home&c=User&a=verify&r=0.9698092918823165")
print(response.content)
print(response.cookies)
# 使用session对象发送登录接口请求
response = self.session.post(url="http://localhost/index.php?
m=Home&c=User&a=do_login&t=0.8916582864091132",
data={"username": "13800138006", "password":
"123456", "verify_code": "8888"},
headers={"Content-Type": "application/x-www-form-
urlencoded"})
# 打印登录的结果
print(response.json())
断言、jsonpath断言
断言查询所有课程接口中,返回的json响应数据里面,id为15的courseName是大数据云计算
self.assertEqual("大数据云计算", jsonpath.jsonpath(response.json(), "$.content[?
(@.id==15)].courseName")[0] )
更多点击链接: