requests基础
一、简介
requests是Python第三方库,而且是应用最广泛的接口测试的第三方库。
requests,通过调用这个库的API进行项目接口的调用,从而达到模拟用户代理的作用,就是代理浏览器向服务器发送请求,并接收响应的工具。
- 安装方式:
pip install requests
二、Postman生成Python代码
2.1 GET

2.2 POST

三、Requests库常见用法
3.1 GET请求
- 语法 ```python import requests
r = requests.get(url=””,params=””,headers={}) print(r.text)
-字段解析```pythonurl : 接口的请求地址params : 请求体,可以为json,也可以拼接字符串(仅限GET接口)headers : 请求头,json格式,如果接口不需要请求头,则可以不传r : 请求对象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形式的数据类型
-执行结果```pythonhttp://localhost:3000/api/customer?_where=(customerphone,eq,18600047517)[{"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"}]<class 'str'>[{'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'}]<class 'list'>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()))
-执行结果```pythonhttp://localhost:3000/api/customer?_where=%28customerphone%2Ceq%2C18600047517%29 # url编码2002.046652[{"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"}]<class 'str'>[{'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'}]<class 'list'>Process finished with exit code 0
3.2 POST请求
3.2.1 表单
- 针对的请求内容类型
Content-Type: application/x-www-form-urlencodedContent-Type: application/form-data
- 语法 ```python import requests
r = requests.post(url=””,data=””,headers={}) # 或request.request(“POST”,url=””,data=””,headers={}) print(r.text)
<a name="c3ef312a"></a>#### 3.2.1.1 x-www-form-urlencoded拼接字符串传递(不推荐)-代码示例```pythonimport requestsurl = "http://localhost:8080/woniusales/customer/add"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' # 此处为拼接字符串形式headers = {'Cookie': 'JSESSIONID=E2F7DEE3F50DE5AA02B59FCAA2536324; Pycharm-39ceae25=f505f80e-880b-4388-999a-98a8d9e99e4b; _jfinal_captcha=9d0fa9d7b11e42c085afa2536ad78ce7; username=admin; password=Milor123; password=Milor123; username=admin','Content-Type': 'application/x-www-form-urlencoded'}response = requests.request("POST", url, headers=headers, data=payload)print(response.text)
- 执行结果 ```python add-successful
Process finished with exit code 0
<a name="f097b932"></a>#### 3.2.1.2 x-www-form-urlencoded字典形式传递-代码示例```pythonimport requestsurl = "http://localhost:8080/woniusales/customer/add"payload={'customerphone':'18600066169','customername':'测试','childsex':'男','childdate':'2010-05-05 00:00:00','creditkids':0,'creditcloth':0}headers = {'Cookie': 'JSESSIONID=E2F7DEE3F50DE5AA02B59FCAA2536324; Pycharm-39ceae25=f505f80e-880b-4388-999a-98a8d9e99e4b; _jfinal_captcha=9d0fa9d7b11e42c085afa2536ad78ce7; username=admin; password=Milor123; password=Milor123; username=admin','Content-Type': 'application/x-www-form-urlencoded'}response = requests.request("POST", url, headers=headers, data=payload)print(response.text)
- 执行结果 ```python add-successful
Process finished with exit code 0
<a name="adf2f94a"></a>#### 3.2.1.3 form-data字典形式传递-示例代码```pythonimport requestsurl = "http://jsonplaceholder.typicode.com/posts"payload={'userId': '1','title': '测试222','body': '测试222'}headers = {'Content-Type': 'application/form-data'}response = requests.request("POST", url, headers=headers, data=payload)print(response.text)
- 执行结果 ```python { “id”: 101 }
Process finished with exit code 0
<a name="080443de"></a>### 3.2.2 JSON-代码示例```pythonimport jsonimport requestsurl = "http://jsonplaceholder.typicode.com/posts"payload = {"userId": 1,"title": "测试222","body": "测试222"}headers = {'Content-Type': 'application/json'}response = requests.request("POST", url, headers=headers, json=payload) # 注意此处使用的是json=,当接口限制必须以JSON格式传递时,要么将字典序列化转为JSON,要么在传参时不能使用data=,只能使用json=print(response.text)
- 执行结果 ```python { “userId”: 1, “title”: “测试222”, “body”: “测试222”, “id”: 101 }
Process finished with exit code 0 ```
