from fastapi import FastAPI, HTTPException, Request from fastapi.middleware.cors import CORSMiddleware import requests app = FastAPI() # Add CORS middleware to allow all origins. Adjust this in production! app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.get("/proxy/") @app.post("/proxy/") async def proxy(request: Request, url: str): if not url: raise HTTPException(status_code=400, detail="URL not provided") # Forward the request to the target URL if request.method == "GET": response = requests.get(url) elif request.method == "POST": # Read JSON data if available data = await request.json() if request.headers.get("Content-Type") == "application/json" else None response = requests.post(url, json=data) # Return the response from the target server return Response(content=response.content, status_code=response.status_code, headers=dict(response.headers)) if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)