58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
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__':
|
||
url = 'https://83aa2db8e006.ngrok-free.app/api/style/callback'
|
||
|
||
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)
|