Adding CORSMiddleware #742
-
I am trying to add CORSMiddleware but having some trouble to implement it. I followed the instructions in the manual. However, I think it is not accepting the syntax in the manual since it cannot iterate over the parameters added to the StacApi class.
After this and some digging in the FastAPI docs, I implemented it after the StacApi class and appended the middleware after it. api = StacApi(
settings=settings,
extensions=extensions,
client=CoreCrudClient(post_request_model=post_request_model), # type: ignore
response_class=ORJSONResponse,
items_get_request_model=items_get_request_model,
search_get_request_model=get_request_model,
search_post_request_model=post_request_model,
)
app = api.app
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["OPTIONS", "GET"],
allow_headers=["Content-Type", "Authorization"],
) I now get a different error and still my stac-browser cannot view my API because of CORS.
My STAC Browser error: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@nikkopante by default, the StacApi will already add a CORSMiddleware stac-fastapi/stac_fastapi/api/stac_fastapi/api/app.py Lines 113 to 121 in e376e30 try from starlette.middleware import Middleware
api = StacApi(
settings=settings,
extensions=extensions,
client=CoreCrudClient(post_request_model=post_request_model), # type: ignore
response_class=ORJSONResponse,
items_get_request_model=items_get_request_model,
search_get_request_model=get_request_model,
search_post_request_model=post_request_model,
middlewares=[
Middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["OPTIONS", "GET"],
allow_headers=["Content-Type", "Authorization"],
),
],
) |
Beta Was this translation helpful? Give feedback.
-
Thank you @vincentsarago! I got it working without the errors now. However, my locally deployed stac-browser cannot view my API yet. https://api.catalog.data.philsa.gov.ph/ Is there any chance, you may know what is causing this issue? |
Beta Was this translation helpful? Give feedback.
@nikkopante by default, the StacApi will already add a CORSMiddleware
stac-fastapi/stac_fastapi/api/stac_fastapi/api/app.py
Lines 113 to 121 in e376e30
try