技能标识:api-tester
Test REST APIs with customizable headers, authentication, and request bodies. Use when debugging API endpoints, testing authentication flows, validating responses, or automating API checks. Supports GET, POST, PUT, DELETE, PATCH methods with JSON, form-data, and raw body formats. Ideal for developers testing webhooks, microservices, and third-party integrations.
使用自定义标头、身份验证和请求体测试 REST API。
python
import requests
def test_get(url, headers=None):
测试 GET 端点
try:
response = requests.get(url, headers=headers, timeout=30)
return {
status: response.status_code,
headers: dict(response.headers),
body: response.json() if response.headers.get(content-type, ).startswith(application/json) else response.text,
time: response.elapsed.total_seconds()
}
except Exception as e:
return {error: str(e)}
python
def test_post(url, data, headers=None):
测试带 JSON 体的 POST 端点
default_headers = {Content-Type: application/json}
if headers:
default_headers.update(headers)
try:
response = requests.post(
url,
json=data,
headers=default_headers,
timeout=30
)
return {
status: response.status_code,
body: response.json() if response.headers.get(content-type, ).startswith(application/json) else response.text
}
except Exception as e:
return {error: str(e)}
python
def testwithauth(url, token=None, username=None, password=None):
使用 Bearer 令牌或基本身份验证测试 API
headers = {}
if token:
headers[Authorization] = fBearer {token}
elif username and password:
import base64
credentials = base64.b64encode(f{username}:{password}.encode()).decode()
headers[Authorization] = fBasic {credentials}
return test_get(url, headers)
python
def comprehensiveapitest(base_url, endpoints):
测试多个端点
results = {}
for endpoint, config in endpoints.items():
url = f{base_url}{config[path]}
method = config.get(method, GET)
headers = config.get(headers, {})
data = config.get(data)
try:
if method == GET:
response = requests.get(url, headers=headers, timeout=30)
elif method == POST:
response = requests.post(url, json=data, headers=headers, timeout=30)
elif method == PUT:
response = requests.put(url, json=data, headers=headers, timeout=30)
elif method == DELETE:
response = requests.delete(url, headers=headers, timeout=30)
results[endpoint] = {
status: response.status_code,
success: 200 <= response.status_code < 300,
time: response.elapsed.total_seconds()
}
except Exception as e:
results[endpoint] = {error: str(e), success: False}
return results
comprehensiveapitest(https://api.example.com, endpoints)
python
from flask import Flask, request
def createwebhooklistener(port=5000):
创建本地 webhook 接收器用于测试
app = Flask(name)
received_data = []
@app.route(/webhook, methods=[POST])
def webhook():
data = request.json
received_data.append(data)
print(f收到 webhook: {data})
return {status: ok}
@app.route(/received, methods=[GET])
def get_received():
return {data: received_data}
return app
python
import time
def testapiperformance(url, iterations=10):
测试 API 响应时间
times = []
for _ in range(iterations):
start = time.time()
requests.get(url, timeout=30)
times.append(time.time() - start)
return {
min: min(times),
max: max(times),
avg: sum(times) / len(times),
times: times
}
python
def validateresponse(response, expectedstatus=200, required_fields=None):
验证 API 响应结构
errors = []
if response.get(status) != expected_status:
errors.append(f期望状态码 {expected_status},实际得到 {response.get(status)})
body = response.get(body, {})
if required_fields:
for field in required_fields:
if field not in body:
errors.append(f缺少必填字段: {field})
return {
valid: len(errors) == 0,
errors: errors
}
bash
pip install requests flask
以下为平台配置的接入选项,并非逐项实测通过。能否安装取决于客户端支持、技能来源和运行环境:
帮我安装 SkillHub 和 rest-api-tester-1776276468 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 rest-api-tester-1776276468 技能
skillhub install rest-api-tester-1776276468
文件大小: 2.53 KB | 发布时间: 2026-4-16 16:17