ka1kuk commited on
Commit
a2ea771
·
1 Parent(s): ac6ba74

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +6 -6
main.py CHANGED
@@ -1,4 +1,4 @@
1
- from fastapi import FastAPI, HTTPException, Query
2
  from fastapi.middleware.cors import CORSMiddleware
3
  import httpx
4
 
@@ -13,17 +13,17 @@ app.add_middleware(
13
  allow_headers=["*"],
14
  )
15
 
16
- @app.get("/")
17
- async def proxy(url: str = Query(..., description="URL to proxy")):
18
  headers = {
19
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
20
  }
21
 
22
  async with httpx.AsyncClient() as client:
23
  try:
24
- response = await client.get(url, headers=headers)
25
- return response.content
26
- except httpx.RequestError as e:
27
  raise HTTPException(status_code=500, detail="Unexpected error")
28
 
29
  if __name__ == "__main__":
 
1
+ from fastapi import FastAPI, HTTPException, Path
2
  from fastapi.middleware.cors import CORSMiddleware
3
  import httpx
4
 
 
13
  allow_headers=["*"],
14
  )
15
 
16
+ @app.get("/{url_path:path}")
17
+ async def proxy(url_path: str = Path(..., description="URL to proxy")):
18
  headers = {
19
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
20
  }
21
 
22
  async with httpx.AsyncClient() as client:
23
  try:
24
+ response = await client.get(url_path, headers=headers)
25
+ return response.json() # Parse the content as JSON and return
26
+ except (httpx.RequestError, ValueError) as e:
27
  raise HTTPException(status_code=500, detail="Unexpected error")
28
 
29
  if __name__ == "__main__":