Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/docs → Failed to load API definition - Fetch Error - Unauthorized /openapi.json #1

Open
pietz opened this issue Apr 6, 2022 · 6 comments

Comments

@pietz
Copy link

pietz commented Apr 6, 2022

Thank you for providing this example. I believe the template doesn't apply here.

Everything works as described however when trying to access the OpenAPI definition through /docs, I'm getting the following error:

Screenshot 2022-04-06 at 20 37 40

I'm trying to do this on AuthLevel "Function" and couldn't find any other references to this problem.

@AntoineLyan
Copy link

I have the same problem with AuthLevel = Function, Did someone find the solution?

@hcs-bernardo-rufino
Copy link

Same problem here with AuthLevel = Function... anyone?

@pietz
Copy link
Author

pietz commented Sep 26, 2022

Slightly dirty workaround that makes it possible to view the docs but doesn't allow to "Try out" the API.

from fastapi.openapi.docs import get_swagger_ui_html

app = FastAPI(docs_url=None)

@app.get("/docs", include_in_schema=False)
async def get_docs(code: str):
    openapi_url = "/openapi.json?code=" + code
    return get_swagger_ui_html(openapi_url=openapi_url, title="docs")

@tonybaloney
Copy link
Contributor

shreyabatra4 pushed a commit that referenced this issue Mar 9, 2023
@farrukh-t
Copy link

If you want to use the Try it out feature, you can use APIKeyHeader and Security to define the x-functions-key header:

from fastapi import FastAPI, Security
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.security import APIKeyHeader

app = FastAPI(docs_url=None)

functions_header = APIKeyHeader(
    name="x-functions-key", description="Azure Functions key"
)


@app.get("/docs", include_in_schema=False)
async def get_docs(code: str | None = None):
    if not code:
        code = ""
    openapi_url = "/openapi.json?code=" + code
    return get_swagger_ui_html(openapi_url=openapi_url, title="docs")


@app.get("/items/{item_id}")
async def get_items(item_id: str, key: str = Security(functions_header)) -> dict:
    return {item_id: "Foo"}

When deployed to Azure Functions, you would still have to pass the Functions key as a query parameter when accessing the /docs endpoint, like /docs?code=your-functions-key, but you'll be able to use the Authorize button to pass the functions key and try out the API

@khalistoo
Copy link

I've tried @farrukh-t and @pietz suggestions above above (if the try doesn't not work is not a biggy but at least having the docs working) but I am just getting constantly the same Failed to load API definition.

import azure.functions as func

from WrapperFunction import app as fastapi_app

app = func.AsgiFunctionApp(app=fastapi_app, http_auth_level=func.AuthLevel.FUNCTION)
from fastapi import FastAPI, Security
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.security import APIKeyHeader

app = FastAPI(docs_url=None)

functions_header = APIKeyHeader(
    name="x-functions-key", description="Azure Functions key"
)


@app.get("/docs", include_in_schema=False)
async def get_docs(code: str | None = None):
    if not code:
        code = ""
    openapi_url = "/openapi.json?code=" + code
    return get_swagger_ui_html(openapi_url=openapi_url, title="docs")


@app.get("/sample")
async def hello_world(key: str = Security(functions_header)) -> dict:
    return {"message": "Hello World"}

The 'code' is passed to the docs endpoint but error is the same.

image

Am I missing something or did things change since the above comment were posted ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants