Fastapi: [BUG] Authorization header is not sent through swagger

Created on 25 Feb 2020  路  9Comments  路  Source: tiangolo/fastapi

It seems that swagger interface cannot send Authorization header.

I added test_auth endpoint with required header authorization like that:

async def check_auth(
        authorization: str = Header(...)
):
    pass

@router.post("/test_auth")
async def test_auth(
        auth = Depends(check_auth)
):
    pass

This works perfectly if I use REST-client with set Authorization header. But, if I use swagger UI it seems that Authorization header always missing. Server responses with status code 422 and:

{ "detail": [ { "loc": [ "header", "authorization" ], "msg": "field required", "type": "value_error.missing" } ] }

Another observation is if I change the header name to something else (not authorization) it works. But my goal is using Authorization...

FastAPI version: 0.49.0

bug

All 9 comments

It is duplication of #612 but it was closed despite that it wasn't solved...

Not sure if you have read: https://github.com/tiangolo/fastapi/issues/612#issuecomment-547886504

For Authorization you can use FastAPI's HTTPBearer.

That sets the required Authorization header in the right place in OpenAPI and references it from your path operation using it.

@phy25 I've read. May you provide a small example using HTTPBearer, please? (there is no even a mention of HTTPBearer in the manual)

If you want to log an user maybe you should try OAuth2PasswordBearer

@alvarogf97 Just need to check JWT token. No user.

Take a look of this example https://github.com/tiangolo/fastapi/issues/1034#issuecomment-591651300

Thanks for the help here @phy25 and @alvarogf97 ! :cake: :bow:

If that answers your question @symonsoft , you can then close the issue.

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Was this page helpful?
0 / 5 - 0 ratings