Search code examples
pythonfastapiuvicorn

Facing issue while loading FastAPI swagger URL


I am trying to run FastAPI application using uvicorn and I am using Python for this. After running the command "uvicorn app.main:app --host 0.0.0.0 --port 8000 --log-level debug", I am able to get the "Hello" message while accessing the URL but the moment I try to access the swagger document it shows the below error:

Fetch error:

Failed to fetch http://0.0.0.0:8000/openapi.json

Fetch error:

Possible cross-origin (CORS) issue? The URL origin (http://0.0.0.0:8000) does not match the page (http://127.0.0.1:8000). Check the server returns the correct 'Access-Control-Allow-*' headers.

Could anyone please help as I am new to this and kind of struggling.

I am expecting the swagger document to load successfully.


Solution

  • You can try the following things:

      uvicorn app.main:app --host 0.0.0.0 --port 8000 --log-level debug
    

    You can access the service now from any IP-Adress. Maybe also check your firewall settings. You can try to accept incoming calls from the port 8000 (outcoming and incoming rules)

    If you need to access the Swagger UI from a different origin or domain, you may need to configure CORS in your FastAPI application. You can do this by using the fastapi.middleware.cors.CORSMiddleware: