Browse Source

增加api参数错误报错

Sherlock1011 10 tháng trước cách đây
mục cha
commit
2b2ed84c4a
2 tập tin đã thay đổi với 19 bổ sung2 xóa
  1. 18 1
      api.py
  2. 1 1
      api_test.py

+ 18 - 1
api.py

@@ -1,4 +1,6 @@
-from fastapi import FastAPI, Request, status, BackgroundTasks, HTTPException
+from fastapi import FastAPI, Request, status
+from fastapi.exceptions import RequestValidationError
+from fastapi.responses import JSONResponse
 import json
 from pydantic import BaseModel
 from utils import Service
@@ -6,6 +8,21 @@ import uvicorn
 
 app = FastAPI()
 
+# 添加全局异常处理器
+@app.exception_handler(RequestValidationError)
+async def validation_exception_handler(request: Request, exc: RequestValidationError):
+    return JSONResponse(
+        status_code=status.HTTP_400_BAD_REQUEST,
+        content={
+            "code": 400,
+            "msg": "请求参数错误",
+            "data": {
+                "detail": exc.errors(),
+                "body": exc.body
+            }
+        },
+    )
+
 # 定义请求体
 class KeyWordRequest(BaseModel):
     brand_name: str                             # 判定品牌名称

+ 1 - 1
api_test.py

@@ -25,7 +25,7 @@ import json
 # print(response.json())
 
 
-url = "http://172.18.1.189:7860/brandanalysis/api/v1/logo"
+url = "https://670813644644357-http-7860.northwest1.gpugeek.com:8443/brandanalysis/api/v1/logo"
 payload = {
     "brand_name": "李宁",
     "image_url": "http://h2.appsimg.com/a.appsimg.com/upload/merchandise/pdcvis/2024/12/13/131/82a925f9-14af-4be3-bf78-3262f7909482.jpg",