I want to retrieve a specific header from my API inside a function with fastAPI, but I can't found a solution for this.
In flask was simply: request.headers['your-header-name']
Why the hell with fastAPI is so complicated to do a simple thing like this?
Anyone know a solution to retrieve a header? Thanks :)
The decorator:
def token_required(f):
@wraps(f)
def decorator(*args, **kwargs):
CONFIG = settings.read_config()
token = None
headers = Request.headers
if "Authorization" in headers:
auth_header = Request.headers
token = auth_header
elif not token:
return {"Error": "Token is missing or incorrect header name"}, 401
try:
public_key = CONFIG["APPLICATION"]["PUBLIC_KEY"]
claim = jwt.decode(token, public_key)
claim.validate()
except UnicodeDecodeError as err:
return {"Error": f"An error occurred -> {err} check your token"}, 401
return f(*args, **kwargs)
return decorator
I need to read 'Authorization' header to check if exist or not.