Skip to content

Cannot reach load docs - openapi spec not found (swagger/redoc) when using root_path and no proxy #2949

@adamcunnington

Description

@adamcunnington

First check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Example

Here's a self-contained, minimal, reproducible, example with my use case:

import fastapi


api = fastapi.FastAPI(root_path="/api")
router = fastapi.APIRouter()
api.include_router(router, prefix="/v1")

uvicorn main:api --reload --debug

Description

  • Open the browser and call the endpoint /docs.
  • It returns the swagger doc but with an error, "Not Found /api/openapi.json"
  • I see in my terminal 2 requests, the second being a call from swagger.
INFO:     127.0.0.1:43074 - "GET /api/docs HTTP/1.1" 200 OK
INFO:     127.0.0.1:43074 - "GET /api/api/openapi.json HTTP/1.1" 404 Not Found

The docs pre-empts this problem here but doesn't suggest a fix beyond the assumption that the reason for root_path usage is that a proxy is involved. What about the normal use case of serving the api under some common prefix, without a proxy in front?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions