0

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.

2
  • What's the reason for using 0.0.0.0 at all and not just using 127.0.0.1? If you plan to use different (local) ips for accessing the service, you'll have to configure CORS as shown in the FastAPI docs for those alternative ips: fastapi.tiangolo.com/tutorial/cors Commented Aug 20, 2024 at 8:01
  • You need to add more details on your app. Do you see the same error if you run simple app like this fastapi.tiangolo.com/#example ? Commented Aug 20, 2024 at 8:21

1 Answer 1

0

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-Address. 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:

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your suggestion. I tried changing the URL and used the above configuration mentioned and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.