用Python写一个接口自动化,举例
时间: 2024-04-10 18:15:54 浏览: 89
以下是一个用Python编写的简单的接口自动化示例:
import requests
# 发送GET请求
response = requests.get('https://jsonplaceholder.typicode.com/posts')
print(response.status_code) # 打印响应状态码
print(response.json()) # 打印响应内容
# 发送POST请求
data = {'title': 'foo', 'body': 'bar', 'userId': 1}
response = requests.post('https://jsonplaceholder.typicode.com/posts', data=data)
print(response.status_code) # 打印响应状态码
print(response.json()) # 打印响应内容
# 发送PUT请求
data = {'title': 'foo', 'body': 'bar'}
response = requests.put('https://jsonplaceholder.typicode.com/posts/1', data=data)
print(response.status_code) # 打印响应状态码
print(response.json()) # 打印响应内容
# 发送DELETE请求
response = requests.delete('https://jsonplaceholder.typicode.com/posts/1')
print(response.status_code) # 打印响应状态码
阅读全文