from fastapi import APIRouter, Request, Path, Query, Body, Form
import aioredis
from pydantic import BaseModel, Field
router = APIRouter()
@router.get("/health", tags=["health"])
async def health():
return {
"status_code": 200,
"msg": "success"
}
class Item(BaseModel):
id: str
title: str
@router.post("/request-data/{id}")
async def request_data(
request: Request,
item: Item,
id: str=Path(..., title="id", description="路径参数"),
query_params: str=Query(..., description="查询参数"),
# form_params: str=Form(..., title="表单", description="表单")
):
result = {
"url": request.url._url, # 请求url
"base_url": request.base_url, # 请求路径
"hostname": request.url.hostname, # 请求 host
"url_port": request.url.port, # 请求端口
"path": request.url.path, # 请求 path
"query": request.url.query, # 查询参数
"is_disconnected": await request.is_disconnected(),
"body": await request.body(), # bytes 请求体
"form": await request.form(), # 表单
"json": await request.json(), # 请求body
"path_params": request.path_params, # 路径参数
"query_params": request.query_params, # 请求参数
"client_port": request.client.port, # 客户端端口
"client_host": request.client.host, # 客户端域名
"method": request.method, # 请求方法
"headers": request.headers, # 请求头
"cookies": request.cookies # cookies
}
return result
"""
{
"url": "http://10.33.33.33:8086/request-data/路径参数?query_params=%E6%9F%A5%E8%AF%A2%E5%8F%82%E6%95%B0",
"base_url": {
"_url": "http://10.33.33.33:8086/"
},
"hostname": "10.33.33.33",
"url_port": 8086,
"path": "/request-data/路径参数",
"query": "query_params=%E6%9F%A5%E8%AF%A2%E5%8F%82%E6%95%B0",
"is_disconnected": false,
"body": "{\n \"id\": \"请求体id\",\n \"title\": \"请求体title\"\n}",
"form": {},
"json": {
"id": "请求体id",
"title": "请求体title"
},
"path_params": {
"id": "路径参数"
},
"query_params": {
"query_params": "查询参数"
},
"client_port": 50378,
"client_host": "10.9.194.135",
"method": "POST",
"headers": {
"host": "10.138.35.88:8086",
"connection": "keep-alive",
"content-length": "54",
"accept": "application/json",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",
"content-type": "application/json",
"origin": "http://10.33.33.33:8086",
"referer": "http://10.33.33.33:8086/docs",
"accept-encoding": "gzip, deflate",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8"
},
"cookies": {}
}
"""