Skip to content

Commit 2f27a68

Browse files
committed
Merge branch 'lfpratik-handle_request_path'
2 parents 600b4af + 4953fe3 commit 2f27a68

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

serverless_wsgi.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ def generate_response(response, event):
162162
return returndict
163163

164164

165+
def strip_express_gateway_query_params(path):
166+
"""Contrary to regular AWS lambda HTTP events, Express Gateway
167+
(https://github.com/ExpressGateway/express-gateway-plugin-lambda)
168+
adds query parameters to the path, which we need to strip.
169+
"""
170+
if "?" in path:
171+
path = path.split("?")[0]
172+
return path
173+
174+
165175
def handle_request(app, event, context):
166176
if event.get("source") in ["aws.events", "serverless-plugin-warmup"]:
167177
print("Lambda warming event received, skipping handler")
@@ -191,7 +201,7 @@ def handle_payload_v1(app, event, context):
191201
# If a user is using a custom domain on API Gateway, they may have a base
192202
# path in their URL. This allows us to strip it out via an optional
193203
# environment variable.
194-
path_info = event["path"]
204+
path_info = strip_express_gateway_query_params(event["path"])
195205
base_path = os.environ.get("API_GATEWAY_BASE_PATH")
196206
if base_path:
197207
script_name = "/" + base_path
@@ -243,7 +253,7 @@ def handle_payload_v2(app, event, context):
243253

244254
script_name = get_script_name(headers, event.get("requestContext", {}))
245255

246-
path_info = event["rawPath"]
256+
path_info = strip_express_gateway_query_params(event["rawPath"])
247257

248258
body = event.get("body", "")
249259
body = get_body_bytes(event, body)
@@ -294,7 +304,7 @@ def handle_lambda_integration(app, event, context):
294304

295305
script_name = get_script_name(headers, event)
296306

297-
path_info = event["requestPath"]
307+
path_info = strip_express_gateway_query_params(event["requestPath"])
298308

299309
for key, value in event.get("path", {}).items():
300310
path_info = path_info.replace("{%s}" % key, value)

0 commit comments

Comments
 (0)