Die Cloud Run POST-Anfrage schlägt mit CORS Preflight 405 (FastAPI-Backend) fehl.Python

Python-Programme
Anonymous
 Die Cloud Run POST-Anfrage schlägt mit CORS Preflight 405 (FastAPI-Backend) fehl.

Post by Anonymous »

Ich habe ein FastAPI-Backend auf Google Cloud Run und ein Vite + React-Frontend bereitgestellt. Ich rufe einen POST-Endpunkt /rag vom Frontend aus auf.
Das erste Problem besteht darin, dass die Swagger-Benutzeroberfläche in FastAPI eine alte Antwort anzeigt:
Image

Die oben gezeigte Swagger-Benutzeroberfläche wird von der Cloud Run-URL /docs bereitgestellt, nicht von eine lokale FastAPI-Instanz.
CURL-Befehl:

Code: Select all

curl -X 'POST' \
'https://vci-188125714482.asia-south1.run.app/rag' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"query": "yo"
}'
Anfrage-URL:

Code: Select all

https://vci-188125714482.asia-south1.run.app/rag
Antworttext:

Code: Select all

{
"answer": "AI result for: yo"
}
Antwortheader:

Code: Select all

 alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
content-length: 30
content-type: application/json
date: Mon,29 Dec 2025 11:33:43 GMT
server: Google Frontend
x-cloud-trace-context: 2b70f32dff26dc2279b45c2643b5f0b5;o=1
Es sollte jedoch You ask: yo angezeigt werden, wie in vc-rag-backend/main.py definiert:

Code: Select all

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from fastapi.responses import JSONResponse

app = FastAPI()

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=False,
allow_methods=["*"],     # MUST be *
allow_headers=["*"],     # MUST be *
)

class Query(BaseModel):
query: str

@app.post("/rag")
async def rag(data: Query):
return {
"answer": f"You asked: {data.query}",
"sources": []
}
Ich habe Dinge wie NPM Run Build, Firebase Deploy und die Neuerstellung der Cloud Run-Revision ausprobiert, aber stattdessen wird immer noch das AI-Ergebnis für: yo angezeigt.
Die Repository-Struktur sieht so aus:

Code: Select all

VCI/
├── main.py                (old backend code)
├── vc-rag-backend/
│   ├── main.py            (new backend code shown above)
│   ├── Dockerfile
│   └── requirements.txt
Das zweite Problem besteht darin, dass die Browser-Preflight-OPTIONS-Anfrage
an /rag eine 405-Antwort zurückgibt, was zu einem CORS-Fehler im Frontend führt:
Image

Cloud Run wird über einen Cloud Build-Trigger bereitgestellt, der mit verbunden ist GitHub. Der Trigger ist für die Verwendung einer Docker-Datei konfiguriert, aber ich bin mir nicht sicher, ob er aus dem Repository-Stammverzeichnis oder aus vc-rag-backend/ erstellt wird.
Wie behebe ich diese beiden Probleme?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post