python3接口自动化--requests库的使用方法

Posted 后来者2012

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3接口自动化--requests库的使用方法相关的知识,希望对你有一定的参考价值。

一. requests库的介绍与安装

pip install requests

二. 请求数据场景

2.1 请求方式:GET, 请求类型:application/x-www-form-urlencoded

# -*- coding: utf-8 -*-
# @Time    : 2021/5/11 23:25
# @Author  : chinablue
# @File    : tmp0512.py

import requests

req_data = {\'key1\': \'value1\', \'key2\': \'value2\'}
resp = requests.get("http://httpbin.org/get", params=req_data)

print(f"请求地址:{resp.url}")
print(f"请求方式:{resp.request.method}")
print(f"请求类型:{resp.request.headers.get(\'Content-Type\')}")  

执行结果

请求地址:http://httpbin.org/get?key1=value1&key2=value2
请求方式:GET
请求类型:None  #默认的Content-Type为application/x-www-form-urlencoded

2.2 请求方式:POST, 请求类型:application/x-www-form-urlencoded

# -*- coding: utf-8 -*-
# @Time    : 2021/5/11 23:25
# @Author  : chinablue
# @File    : tmp0512.py

import requests

req_data = {\'key1\': \'value1\', \'key2\': \'value2\'}
resp = requests.post("http://httpbin.org/post", data=req_data)

print(f"请求地址:{resp.url}")
print(f"请求方式:{resp.request.method}")
print(f"请求类型:{resp.request.headers.get(\'Content-Type\')}")
print(f"请求数据:{resp.request.body}")

执行结果

请求地址:http://httpbin.org/post
请求方式:POST
请求类型:application/x-www-form-urlencoded
请求数据:key1=value1&key2=value2

2.3 请求方式:POST, 请求类型:application/json

# -*- coding: utf-8 -*-
# @Time    : 2021/5/11 23:25
# @Author  : chinablue
# @File    : tmp0512.py

import requests

req_data = {\'key1\': \'value1\', \'key2\': \'value2\'}
resp = requests.post("http://httpbin.org/post", json=req_data)
# resp = requests.put("http://httpbin.org/put", json=req_data)
# resp = requests.delete("http://httpbin.org/delete", json=req_data)

print(f"请求地址:{resp.url}")
print(f"请求方式:{resp.request.method}")
print(f"请求类型:{resp.request.headers.get(\'Content-Type\')}")
print(f"请求数据:{resp.request.body}")

执行结果

请求地址:http://httpbin.org/post
请求方式:POST
请求类型:application/json
请求数据:b\'{"key1": "value1", "key2": "value2"}\'

2.4 请求方式:POST, 请求类型:multipart/form-data; boundary=xxxx

# -*- coding: utf-8 -*-
# @Time    : 2021/5/11 23:25
# @Author  : chinablue
# @File    : tmp0512.py

import requests

req_data = {\'key1\': \'value1\', \'key2\': \'value2\'}
resp = requests.post("http://httpbin.org/post", files=req_data)

print(f"请求地址:{resp.url}")
print(f"请求方式:{resp.request.method}")
print(f"请求类型:{resp.request.headers.get(\'Content-Type\')}")
print(f"请求数据:{resp.request.body}")

执行结果

请求地址:http://httpbin.org/post
请求方式:POST
请求类型:multipart/form-data; boundary=a59060b098198d3900189d1e51504c4b
请求数据:b\'--a59060b098198d3900189d1e51504c4b\\r\\nContent-Disposition: form-data; name="key1"; filename="key1"\\r\\n\\r\\nvalue1\\r\\n--a59060b098198d3900189d1e51504c4b\\r\\nContent-Disposition: form-data; name="key2"; filename="key2"\\r\\n\\r\\nvalue2\\r\\n--a59060b098198d3900189d1e51504c4b--\\r\\n\'

说明事项:

  1. files参数中如果有文件数据,其格式如下:

files = {
    "file": ("1.png", open("1.png", "rb"), "images/png")
}

  2. 如果上传的文件比较大,建议使用requests-toolbelt库来辅助

小结

  1. requests库支持所有的HTTP请求,如get,post,put,delete等

  2. 发送请求时,有4个参数可以传入请求数据:params(参数在url上),data,json,files

三. 响应数据场景

# -*- coding: utf-8 -*-
# @Time    : 2021/5/11 23:25
# @Author  : chinablue
# @File    : tmp0512.py

import requests

resp = requests.get("http://httpbin.org/get")

print(f"响应状态码: {resp.status_code}")
print(f"响应头信息: {resp.headers}")
print(f"响应cookies信息: {resp.cookies}")
print(f"获取PreparedRequest对象:{resp.request}")
print(f"响应内容(str类型): {resp.text}")
print(f"响应内容(dict类型): {resp.json()}")  #如果响应类型不是json,则抛出异常
print(f"响应内容(bytes类型): {resp.content}")

说明事项:

  1. resp.cookies返回的是一个RequestsCookieJar对象,若将其转为字典对象:

# -*- coding: utf-8 -*-
# @Time    : 2021/5/11 23:25
# @Author  : chinablue
# @File    : tmp0512.py

import requests

resp = requests.get("https://www.baidu.com/img/flexible/logo/pc/result.png")

print(f"响应cookies信息(字典类型): {requests.utils.dict_from_cookiejar(resp.cookies)}")
print(f"响应cookies信息(字典类型): {resp.cookies.get_dict()}")
print(f"响应cookies信息(列表类型): {resp.cookies.items()}")

  2. resp.request返回的是一个PreparedRequest对象,可以从该对象中获取到相关请求信息

print(f"请求地址:{resp.request.url}")
print(f"请求方法:{resp.request.method}")
print(f"请求头信息:{resp.request.headers}")

  3. resp.content返回的是二进制数据,一般用于获取图片或文件数据

# -*- coding: utf-8 -*-
# @Time    : 2021/5/11 23:25
# @Author  : chinablue
# @File    : tmp0512.py

import requests

resp = requests.get("https://www.baidu.com/img/flexible/logo/pc/result.png")

if resp.status_code == requests.codes.ok:
    with open("baidu.png", "wb") as f:
        f.write(resp.content)
else:
    raise Exception(f"请求失败,当前http响应码: {resp.status_code}")

 

以上是关于python3接口自动化--requests库的使用方法的主要内容,如果未能解决你的问题,请参考以下文章

python3+request接口自动化框架

python3.6+requests实现接口自动化6

python3+requests实现接口自动化1

python3+requests接口自动化-日志封装

python3+requests+unittest:接口自动化测试

Python3+requests搭建接口自动化测试框架