Skip to content

Allow custom http header name instead of default "Authorization" #4651

@hanssens

Description

@hanssens

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.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

# see wanted code

Description

The http header for the different authentication protocols is currently hardcoded to "Authorization", which on itself is accurate according to the standards ((ex. MDN definition)[https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization]). There are, however, reasons in practice you would want to have a different, custom named header.

For example, in my case where the FastAPI is hosted behind a proxy which already requires the Authorization header to be set. I would like it to be set in the FastAPI to something else (e.g. like "x-bearer-key" or "foo").

Is it possible to define a custom http header name for authorization, instead of the default "Authorization" header name?

Wanted Solution

example code of what would be great

auth_scheme = HTTPBearer(authorization_header='MY-OWN-KEY-INSTEAD-OF-AUTHORIZATION')

Wanted Code

# example code of what would be great
auth_scheme = HTTPBearer(authorization_header='MY-OWN-KEY-INSTEAD-OF-AUTHORIZATION')

Alternatives

No response

Operating System

Linux, Windows, macOS

Operating System Details

No response

FastAPI Version

0.74

Python Version

3.10.x

Additional Context

No response

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