接口自动化测试之requests 库(学习笔记)

一.requests库简介

Python第三方库,可用于接口自动化测试

二.安装

方式一:命令行模式安装 pip install requests

方式二:Pycharm安装

file-》default settings-》project interpreter-》搜索requests-》install package-》ok

三.发送请求

1.requests.get()  发送get请求

  • 无参数
    import requests
    url = "https://www.baidu.com" #接口地址,这里为了方便说明,随意写的地址
    res = requests.get(url)
    print(res.json()) # 获取接口响应体并按json格式输出
  • 有参数时,地址和参数之间使用 “?” 拼接进行传参
    url = "https://m.douban.com/rexxar/api/v2/gallery/subject_feed?start=0&count=4&subject_id=35376457&ck=null" # 地址和参数之间使用“?”拼接,参数和参数之间使用“&”连接,作为整体URL
    response = requests.get(url)  
    print(response.json())
  • 有参数时,使用params传参,参数可以是字典、元组类型
    url = "http://127.0.0.1:9700/api/data/root/nodes.json"
    param = {"userName":"admin","accountType":"username","clientId":"e741d0c4-a6ee-4b40-99eb-cb78437f885a","projectCode":"school"} # 字典类型的数据
    # param = (("userName","admin"),("accountType","username"),("clientId","e741d0c4-a6ee-4b40-99eb-cb78437f885a"),("projectCode","school")) # 元组类型的数据
    response = requests.get(url=url, params=param)  # 发送get请求,通过params传参
    print(response.json())

2.requests.post() 发送post请求,根据请求头 Content-Type 选择data或json传参

  • 使用data传参。参数可以是键值对的dict类型,也可以是str类型

                Content-Type:application/x-www-urlencoded   表单类型传参,参数为键值对的dict类型

                Content-Type:text/plain  文本类型传参,参数为str类型

  • 使用json传参        

                Content-Type:application/json   参数是键值对或非键值对的dict类型

import requests
url = "https://127.0.0.1:9700/api/user/account/page"
header = {"Authorization":"7de2ba9a-b0ff-4e55-b4e4-4954e492ec98"} 
data = {"type":"teacher","pageNum":1,"pageSize":100000}
res = requests.post(url=url, json=data, headers=header) # 使用json传参,并带有token请求头
print(res.json())

注:

键值对 :value是单个的字符串或数字,例如:{"type":"teacher","pageNum":1,"pageSize":100000} 

非键值对:value可以是列表,字典等,例如:{"tag":{"id":1, "type":"teacher"}}

json字符串与dict之间的转换:

import json
value = {"tag":{"id":1, "type":"teacher"}}
print(type(json.dumps(value))) # 字典==》json字符串,并打印出转换后的类型
data = '{"tag":{"id":1, "type":"teacher"}}'
print(type(json.loads(data))) # 字符串==》字典

3.其他请求

requests.put(),使用data传参

requests.delete()

可通过ctrl+单击函数名 查看原函数,了解函数的用法,如下为put原函数

def put(url, data=None, **kwargs):
    r"""Sends a PUT request.

    :param url: URL for the new :class:`Request` object.
    :param data: (optional) Dictionary, list of tuples, bytes, or file-like
        object to send in the body of the :class:`Request`.
    :param json: (optional) json data to send in the body of the :class:`Request`.
    :param \*\*kwargs: Optional arguments that ``request`` takes.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response
    """
    return request('put', url, data=data, **kwargs)

4.requests.request(method, url, **kwargs)

get,post,put,delete等请求方法都是通过调用request方法实现的,所以可以直接使用request方法发送所有类型的请求。

method: 请求方式,可以是 get,post,put,delete,patch等

url: 请求地址

**kwargs:可选参数

        headers:请求头

        cookies:cookie关联

        files: 文件上传

        其他可选参数可查看request原函数

5.发送带header的请求

import requests
url = "https://127.0.0.1:9700/api/user/account/page"
header = {"Authorization":"7de2ba9a-b0ff-4e55-b4e4-4954e492ec98"}
data = {"type":"teacher","pageNum":1,"pageSize":100000}
res = requests.request(method="post",url=url, json=data, headers=header)
print(res.json())

6.上传文件

import requests
url = "http://127.0.0.1:9700/api/file/image/upload.json"
value = {
    "file": open(r"E:\screenshot\50010619970307961X.png","rb")
}
res = requests.post(url, files=value)
print(res.json())

7.cookie关联 

  • 方式一:从获取cookie接口的返回结果中通过res.cookies获取cookie,用于其他请求使用
php_cookie = res.cookies # 获取cookie

requests.post(url=url, json=data, headers=header, cookies=php_cookie) # 发送请求时携带cookie
  • 方式二:
session = requests.session() # 创建会话,多个请求之间可以通过会话关联cookie

session.request("post",url=url, json=data, headers=header) # 使用会话来发送请求,无需多次重复传cookies参数

四.获取返回结果

res.json() # 将返回值转化成一个dict对象
res.text # 将返回值转化成文本
res.content # 将返回值转化成字节类型数据
res.status_code # 获取响应码
res.reason # 获取返回信息
res.cookies # 返回cookie信息
res.encoding # 编码方式
res.headers # 响应头
res.request.method # 获取请求方式,request包含所有请求数据

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值