Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to add custom headers to Swagger UI requests in AWS Lambda Powertools? #6524

harshitsinghai77 started this conversation in General
Discussion options

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!

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /