CORS Error in FastAPI Backend from React Frontend via APIM

KT 190 Reputation points
2024-11-05T11:11:14.96+00:00

Hi all,

I have a setup with a React frontend App Service and a FastAPI backend App Service on Azure. My goal is to restrict access to the backend so that only the frontend App Service can reach it. However, I encountered a 403 IP forbidden error when trying this approach.

To work around this, I tried using an API Management (APIM) service as a proxy between the frontend and backend. Despite adding the URLs of both the frontend App Service and APIM in the backend's CORS settings, I keep getting CORS errors when accessing the API from the browser. I also configured CORS settings in both the APIM policies and within the FastAPI code.

Notably, this CORS error only occurs when I call the API from the browser; if I make the API call from the SSH terminal in the frontend App Service, it works without issues.

For simplicity, I removed private endpoint restrictions on the backend and changed it to accept all traffic temporarily. I noticed that if I click on the red "message" text, a new tab opens and successfully shows the response from the backend API.

スクリーンショット 2024-11-05 午後8.03.16

Does anyone have suggestions on how to resolve this CORS error when calling the API from the browser?

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,935 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Ifeoluwa Oduwaiye 0 Reputation points
    2024-11-05T12:34:05.94+00:00

    Hi KT,

    Can you confirm if you have enabled a CORS policy on APIM? If not, you can follow the steps here to do so: https://learn.microsoft.com/en-us/azure/api-management/enable-cors-developer-portal?wt.mc_id=studentamb_271760


  2. Shree Hima Bindu Maganti 4,775 Reputation points Microsoft External Staff Moderator
    2024-11-11T04:43:59.76+00:00

    Hi @kT,
    Thanks for sharing your detailed configuration!
    Based on what you've set up, it looks like you've covered most of the things. However, here are a few suggestions that might help resolve the CORS issue when calling the API from the browser:
    Remove "*" in allow_origins: Update the allow_origins in FastAPI to only include your frontend and APIM URLs:

    app.add_middleware(
        CORSMiddleware,
        allow_origins=["https://frontapp.azurewebsites.net", "https://myapim.azure-api.net"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    

    Verify APIM URL: Ensure https://myapim.azure-api.net is the exact URL your frontend is accessing, with correct protocol and no typos.

    Check Preflight Response: Use the browser's developer tools to inspect the OPTIONS preflight request. Confirm that Access-Control-Allow-Origin and other CORS headers are returned correctly.

    Explicitly Define Headers in APIM Policy: Instead of using <header>*</header>, explicitly specify needed headers like Authorization and Content-Type in <allowed-headers> and <expose-headers>.

    Clear Browser Cache: Test in an incognito window or clear the browser cache to avoid cached CORS settings.

    Confirm Backend CORS Settings: Ensure any CORS settings in the backend App Service are consistent with your APIM and Fast API configurations.
    Let me know for any further assistance.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.