requests基础

一、简介

requests是Python第三方库,而且是应用最广泛的接口测试的第三方库。

requests,通过调用这个库的API进行项目接口的调用,从而达到模拟用户代理的作用,就是代理浏览器向服务器发送请求,并接收响应的工具。

  • 安装方式:pip install requests

二、Postman生成Python代码

2.1 GET

接口自动化06-Requests基础 - 图1

2.2 POST

接口自动化06-Requests基础 - 图2

三、Requests库常见用法

3.1 GET请求

  • 语法 ```python import requests

r = requests.get(url=””,params=””,headers={}) print(r.text)

  1. -
  2. 字段解析
  3. ```python
  4. url : 接口的请求地址
  5. params : 请求体,可以为json,也可以拼接字符串(仅限GET接口)
  6. headers : 请求头,json格式,如果接口不需要请求头,则可以不传
  7. r : 请求对象
  8. r.text : 将请求结果以文本(str)的形式打印,如果返回结果是json,则可以使用r.json(),返回数据类型为真实数据类型,如列表则是list,如字典则是dict

3.1.1 拼接字符串形式传递(不推荐)

  • 代码示例 ```python import requests

url = “http://localhost:3000/api/customer?_where=(customerphone,eq,18600047517)” # 此处为拼接字符串形式

payload={} headers = {}

r = requests.request(“GET”, url, headers=headers, params=payload) # 或request.request(“GET”,url=””,params=””,headers={})

print(r.text) # 以文本形式打印 print(type(r.text)) # 查看文本形式的数据类型 print(r.json()) # 以JSON形式打印 print(type(r.json())) # 查看JSON形式的数据类型

  1. -
  2. 执行结果
  3. ```python
  4. http://localhost:3000/api/customer?_where=(customerphone,eq,18600047517)
  5. [{"customerid":113,"customerphone":"18600047517","customername":"黄文星","childsex":"女","childdate":"2010-05-04T16:00:00.000Z","creditkids":0,"creditcloth":0,"credittotal":0,"userid":1,"createtime":"2022-05-09T15:15:44.000Z","updatetime":"2022-05-09T15:15:44.000Z"}]
  6. <class 'str'>
  7. [{'customerid': 113, 'customerphone': '18600047517', 'customername': '黄文星', 'childsex': '女', 'childdate': '2010-05-04T16:00:00.000Z', 'creditkids': 0, 'creditcloth': 0, 'credittotal': 0, 'userid': 1, 'createtime': '2022-05-09T15:15:44.000Z', 'updatetime': '2022-05-09T15:15:44.000Z'}]
  8. <class 'list'>
  9. Process finished with exit code 0

3.1.2 字典形式传递

  • 代码示例 ```python import requests

url = “http://localhost:3000/api/customer

payload={“_where”:”(customerphone,eq,18600047517)”} # 此处为字典形式 headers = {}

r = requests.request(“GET”, url, headers=headers, params=payload)

print(r.url) # 查看请求的url print(r.status_code) # 查看请求的状态码 print(r.elapsed.total_seconds()) # 查看请求耗时,单位秒 print(r.text) print(type(r.text)) print(r.json()) print(type(r.json()))

  1. -
  2. 执行结果
  3. ```python
  4. http://localhost:3000/api/customer?_where=%28customerphone%2Ceq%2C18600047517%29 # url编码
  5. 200
  6. 2.046652
  7. [{"customerid":113,"customerphone":"18600047517","customername":"黄文星","childsex":"女","childdate":"2010-05-04T16:00:00.000Z","creditkids":0,"creditcloth":0,"credittotal":0,"userid":1,"createtime":"2022-05-09T15:15:44.000Z","updatetime":"2022-05-09T15:15:44.000Z"}]
  8. <class 'str'>
  9. [{'customerid': 113, 'customerphone': '18600047517', 'customername': '黄文星', 'childsex': '女', 'childdate': '2010-05-04T16:00:00.000Z', 'creditkids': 0, 'creditcloth': 0, 'credittotal': 0, 'userid': 1, 'createtime': '2022-05-09T15:15:44.000Z', 'updatetime': '2022-05-09T15:15:44.000Z'}]
  10. <class 'list'>
  11. Process finished with exit code 0

3.2 POST请求

3.2.1 表单

  • 针对的请求内容类型
    1. Content-Type: application/x-www-form-urlencoded
    2. Content-Type: application/form-data
  • 语法 ```python import requests

r = requests.post(url=””,data=””,headers={}) # 或request.request(“POST”,url=””,data=””,headers={}) print(r.text)

  1. <a name="c3ef312a"></a>
  2. #### 3.2.1.1 x-www-form-urlencoded拼接字符串传递(不推荐)
  3. -
  4. 代码示例
  5. ```python
  6. import requests
  7. url = "http://localhost:8080/woniusales/customer/add"
  8. payload='customerphone=18600066168&customername=%E9%BB%84%E6%96%87%E6%98%9F&childsex=%E5%A5%B3&childdate=2010-05-05%2000%3A00%3A00&creditkids=0&creditcloth=0' # 此处为拼接字符串形式
  9. headers = {
  10. 'Cookie': 'JSESSIONID=E2F7DEE3F50DE5AA02B59FCAA2536324; Pycharm-39ceae25=f505f80e-880b-4388-999a-98a8d9e99e4b; _jfinal_captcha=9d0fa9d7b11e42c085afa2536ad78ce7; username=admin; password=Milor123; password=Milor123; username=admin',
  11. 'Content-Type': 'application/x-www-form-urlencoded'
  12. }
  13. response = requests.request("POST", url, headers=headers, data=payload)
  14. print(response.text)
  • 执行结果 ```python add-successful

Process finished with exit code 0

  1. <a name="f097b932"></a>
  2. #### 3.2.1.2 x-www-form-urlencoded字典形式传递
  3. -
  4. 代码示例
  5. ```python
  6. import requests
  7. url = "http://localhost:8080/woniusales/customer/add"
  8. payload={
  9. 'customerphone':'18600066169',
  10. 'customername':'测试',
  11. 'childsex':'男',
  12. 'childdate':'2010-05-05 00:00:00',
  13. 'creditkids':0,
  14. 'creditcloth':0
  15. }
  16. headers = {
  17. 'Cookie': 'JSESSIONID=E2F7DEE3F50DE5AA02B59FCAA2536324; Pycharm-39ceae25=f505f80e-880b-4388-999a-98a8d9e99e4b; _jfinal_captcha=9d0fa9d7b11e42c085afa2536ad78ce7; username=admin; password=Milor123; password=Milor123; username=admin',
  18. 'Content-Type': 'application/x-www-form-urlencoded'
  19. }
  20. response = requests.request("POST", url, headers=headers, data=payload)
  21. print(response.text)
  • 执行结果 ```python add-successful

Process finished with exit code 0

  1. <a name="adf2f94a"></a>
  2. #### 3.2.1.3 form-data字典形式传递
  3. -
  4. 示例代码
  5. ```python
  6. import requests
  7. url = "http://jsonplaceholder.typicode.com/posts"
  8. payload={
  9. 'userId': '1',
  10. 'title': '测试222',
  11. 'body': '测试222'
  12. }
  13. headers = {
  14. 'Content-Type': 'application/form-data'
  15. }
  16. response = requests.request("POST", url, headers=headers, data=payload)
  17. print(response.text)
  • 执行结果 ```python { “id”: 101 }

Process finished with exit code 0

  1. <a name="080443de"></a>
  2. ### 3.2.2 JSON
  3. -
  4. 代码示例
  5. ```python
  6. import json
  7. import requests
  8. url = "http://jsonplaceholder.typicode.com/posts"
  9. payload = {
  10. "userId": 1,
  11. "title": "测试222",
  12. "body": "测试222"
  13. }
  14. headers = {
  15. 'Content-Type': 'application/json'
  16. }
  17. response = requests.request("POST", url, headers=headers, json=payload) # 注意此处使用的是json=,当接口限制必须以JSON格式传递时,要么将字典序列化转为JSON,要么在传参时不能使用data=,只能使用json=
  18. print(response.text)
  • 执行结果 ```python { “userId”: 1, “title”: “测试222”, “body”: “测试222”, “id”: 101 }

Process finished with exit code 0 ```