1. from fastapi import APIRouter, Request, Path, Query, Body, Form
    2. import aioredis
    3. from pydantic import BaseModel, Field
    4. router = APIRouter()
    5. @router.get("/health", tags=["health"])
    6. async def health():
    7. return {
    8. "status_code": 200,
    9. "msg": "success"
    10. }
    11. class Item(BaseModel):
    12. id: str
    13. title: str
    14. @router.post("/request-data/{id}")
    15. async def request_data(
    16. request: Request,
    17. item: Item,
    18. id: str=Path(..., title="id", description="路径参数"),
    19. query_params: str=Query(..., description="查询参数"),
    20. # form_params: str=Form(..., title="表单", description="表单")
    21. ):
    22. result = {
    23. "url": request.url._url, # 请求url
    24. "base_url": request.base_url, # 请求路径
    25. "hostname": request.url.hostname, # 请求 host
    26. "url_port": request.url.port, # 请求端口
    27. "path": request.url.path, # 请求 path
    28. "query": request.url.query, # 查询参数
    29. "is_disconnected": await request.is_disconnected(),
    30. "body": await request.body(), # bytes 请求体
    31. "form": await request.form(), # 表单
    32. "json": await request.json(), # 请求body
    33. "path_params": request.path_params, # 路径参数
    34. "query_params": request.query_params, # 请求参数
    35. "client_port": request.client.port, # 客户端端口
    36. "client_host": request.client.host, # 客户端域名
    37. "method": request.method, # 请求方法
    38. "headers": request.headers, # 请求头
    39. "cookies": request.cookies # cookies
    40. }
    41. return result
    42. """
    43. {
    44. "url": "http://10.33.33.33:8086/request-data/路径参数?query_params=%E6%9F%A5%E8%AF%A2%E5%8F%82%E6%95%B0",
    45. "base_url": {
    46. "_url": "http://10.33.33.33:8086/"
    47. },
    48. "hostname": "10.33.33.33",
    49. "url_port": 8086,
    50. "path": "/request-data/路径参数",
    51. "query": "query_params=%E6%9F%A5%E8%AF%A2%E5%8F%82%E6%95%B0",
    52. "is_disconnected": false,
    53. "body": "{\n \"id\": \"请求体id\",\n \"title\": \"请求体title\"\n}",
    54. "form": {},
    55. "json": {
    56. "id": "请求体id",
    57. "title": "请求体title"
    58. },
    59. "path_params": {
    60. "id": "路径参数"
    61. },
    62. "query_params": {
    63. "query_params": "查询参数"
    64. },
    65. "client_port": 50378,
    66. "client_host": "10.9.194.135",
    67. "method": "POST",
    68. "headers": {
    69. "host": "10.138.35.88:8086",
    70. "connection": "keep-alive",
    71. "content-length": "54",
    72. "accept": "application/json",
    73. "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",
    74. "content-type": "application/json",
    75. "origin": "http://10.33.33.33:8086",
    76. "referer": "http://10.33.33.33:8086/docs",
    77. "accept-encoding": "gzip, deflate",
    78. "accept-language": "zh-CN,zh;q=0.9,en;q=0.8"
    79. },
    80. "cookies": {}
    81. }
    82. """