-
Notifications
You must be signed in to change notification settings - Fork 466
How to add custom headers to Swagger UI requests in AWS Lambda Powertools? #6524
-
Hi team 👋
I'm using aws_lambda_powertools.event_handler and have enabled Swagger UI using enable_swagger. I have a use case where I want to add custom headers to the requests triggered from Swagger UI — for example, when a user clicks "Execute" on an endpoint, I want headers like "x-custom-header": "custom-value" to be included automatically.
Here’s the middleware I’ve tried:
def swagger_middleware(app: APIGatewayRestResolver, next_middleware: NextMiddleware) -> Response: print("inside swagger_middleware") if app.current_event.path == "/powertools-swagger": print(f"==>> app.current_event.path: {app.current_event.path}") # Attempting to add custom headers new_headers = dict(app.current_event.headers) new_headers["x-custom-header"] = "custom-value" # Trying to assign new headers app.current_event.headers = new_headers # <-- causes error return next_middleware(app) powertools_app = APIGatewayRestResolver() powertools_app.enable_swagger( path="/powertools-swagger", middlewares=[swagger_middleware] )
However, this results in the following error:
AttributeError: property 'headers' of 'APIGatewayProxyEvent' object has no setter
It seems headers is a read-only property on the event object. I've also tried other approaches, but nothing seems to allow me to inject custom headers into the request that Swagger UI makes to my backend endpoints.
Question:
What’s the recommended or supported way to add custom headers to requests that Swagger UI makes when using enable_swagger()? Ideally, I’d like to set these headers globally without requiring users to input them manually every time.
Thanks in advance!
Beta Was this translation helpful? Give feedback.