Files

58 lines
1.6 KiB
Python
Raw Permalink Normal View History

2025-10-24 10:37:19 +08:00
import json
import time
import requests
def post_request(url, data=None, json_data=None, headers=None, auth=None, timeout=5):
"""
发送POST请求的封装函数
:param url: 接口的URL地址
:param data: 要发送的数据字典形式用于表单数据等会自动编码
:param json_data: 要发送的JSON数据字典形式会自动转换为JSON字符串
:param headers: 请求头字典
:param auth: 认证信息 ('username', 'password') 形式用于基本认证
:param timeout: 超时时间单位为秒
:return: 返回接口的响应对象
"""
try:
response = requests.post(
url,
data=data,
json=json_data,
headers=headers,
auth=auth,
timeout=timeout
)
response.raise_for_status() # 如果请求失败,抛出异常
return response
except requests.RequestException as e:
print(f"POST请求出错: {e}")
return None
if __name__ == '__main__':
2025-10-27 11:29:26 +08:00
url = 'https://0dd6f6504aff.ngrok-free.app/api/style/callback'
2025-10-24 10:37:19 +08:00
object_data = {
'outfit_id': "test",
"status": "test",
"path": "test",
"items": [
"test"
]
}
headers = {
'Accept': "*/*",
'Accept-Encoding': "gzip, deflate, br",
'User-Agent': "PostmanRuntime-ApipostRuntime/1.1.0",
'Connection': "keep-alive",
'Content-Type': "application/json"
}
start_time = time.time()
X = post_request(url=url, data=json.dumps(object_data), headers=headers)
print(time.time() - start_time)
print(X)