【python自动化五】接口自动化底子--requests的使用

[复制链接]
发表于 2025-12-10 20:46:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
python的接口哀求可以用requests库,这个先容就不多说了,网上说得很具体。
接下来直接记录下怎样使用(固然也不限于自动化的使用)
1.安装requests

requests也必要安装一下
  1. pip install requests
复制代码

2.requests哀求

1.常用的哀求方法

接口哀求通常有get/post/put/delete等范例,我们可以直接根据这种范例哀求调用方法,比方:
  1. requests.get(url='')
  2. requests.post(url='', data='')
复制代码
这里我本身写了个简朴的服务,用于测试接口哀求,如下图能正常哀求到接口数据。

固然另有其他方法:

上面是用到哪个范例的哀求调用哪个范例,固然也有同一的方法
  1. requests.request(method='',url='')  # :param method: method for the new :class:`Request` object: ``GET``, ``OPTIONS``, ``HEAD``, ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.
复制代码
在这个方法中传入method参数即可,要调用get接口,就传入‘GET’,要调用post接口,就传入‘POST’。

如上图所示,也能调用乐成。
这里我们也可以看一下get方法的具体调用

实在就是get方法里调用了request方法,全部哀求方法都是基于request方法的。
2.哀求参数

这里附上request方法中解释,内里表明确各个参数的寄义:
  1. """Constructs and sends a :class:`Request <Request>`.
  2.     :param method: method for the new :class:`Request` object: ``GET``, ``OPTIONS``, ``HEAD``, ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.
  3.     :param url: URL for the new :class:`Request` object.
  4.     :param params: (optional) Dictionary, list of tuples or bytes to send
  5.         in the query string for the :class:`Request`.
  6.     :param data: (optional) Dictionary, list of tuples,
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表