question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow custom http header name instead of default "Authorization"

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
ShaharIlanycommented, Jul 7, 2022

Hey!

You can use this instead:

from fastapi import Security
from fastapi.security import APIKeyHeader

@app.get("/auth-needed")
def auth_needed(api_key = Security(APIKeyHeader(name="X-API-Key"))):
    pass

1reaction
Kludexcommented, Jun 3, 2022

It’s not possible due to OpenAPI limitations, not FastAPI ones.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access-Control-Allow-Headers - HTTP - MDN Web Docs
In requests with credentials, it is treated as the literal header name " * " without special semantics. Note that the Authorization header...
Read more >
Custom HTTP headers : naming conventions - Stack Overflow
The recommendation is was to start their name with "X-". E.g. X-Forwarded-For , X-Requested-With . This is also mentioned in a.o. section 5 ......
Read more >
Adding custom headers to origin requests - Amazon CloudFront
If the header names and values that you specify are not already present in the viewer request, CloudFront adds them to the origin...
Read more >
Configuring HTTP Headers that Require Special Treatment
For example, if your application uses custom headers that must occur in every request, you can configure mandatory headers in the security policy....
Read more >
Using Axios to set request headers - LogRocket Blog
We can address this by specifying configuration defaults. This code sets authorization headers for all requests: axios.defaults.headers.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found