tanbushi commited on
Commit
68b968f
·
verified ·
1 Parent(s): 45210db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -4
app.py CHANGED
@@ -3,7 +3,6 @@
3
  from fastapi import FastAPI, Request, HTTPException
4
  import re
5
  import httpx
6
- # import requests
7
 
8
  app = FastAPI()
9
 
@@ -12,7 +11,7 @@ def greet_json():
12
  return {"Hello": "World!"}
13
 
14
  @app.get("/v1/{dest_url:path}")
15
- async def gre_dest_url(dest_url:str):
16
  return dest_url
17
 
18
  @app.post("/v1/{dest_url:path}")
@@ -35,8 +34,11 @@ async def proxy_url(dest_url: str, request: Request):
35
 
36
  # 检查响应状态码
37
  if response.status_code == 200:
38
- # 返回响应内容(假设是 JSON
39
- return response.json()
 
 
 
40
  else:
41
  # 将错误响应转换为 JSON 格式并返回给客户端
42
  try:
 
3
  from fastapi import FastAPI, Request, HTTPException
4
  import re
5
  import httpx
 
6
 
7
  app = FastAPI()
8
 
 
11
  return {"Hello": "World!"}
12
 
13
  @app.get("/v1/{dest_url:path}")
14
+ async def gre_dest_url(dest_url: str):
15
  return dest_url
16
 
17
  @app.post("/v1/{dest_url:path}")
 
34
 
35
  # 检查响应状态码
36
  if response.status_code == 200:
37
+ # 检查响应内容类型是否为 JSON
38
+ if 'application/json' in response.headers.get('content-type', ''):
39
+ return response.json()
40
+ else:
41
+ return {"error": "Response is not in JSON format"}
42
  else:
43
  # 将错误响应转换为 JSON 格式并返回给客户端
44
  try: