Skip to content

Bug: ALBResolver with validation returns 422 for form bodies when multi-value headers are enabled #8949

Backlog

Description

@khaledix3

Expected Behaviour

A POST with Content-Type: application/x-www-form-urlencoded (or multipart/form-data) to an ALBResolver(enable_validation=True) route with Form() parameters returns 200 whether the target group has multi-value headers enabled or not.

Current Behaviour

With multi-value headers enabled, ALB sends only multiValueHeaders (no headers key, as in tests/events/albMultiValueHeadersEvent.json). _get_content_type in openapi_validation.py reads app.current_event.headers, which is empty, so the Content-Type is lost, the form body is parsed as JSON and the request returns 422 json_invalid. The same request with single-value headers returns 200.

Code snippet

from typing import Annotated

from aws_lambda_powertools.event_handler import ALBResolver
from aws_lambda_powertools.event_handler.openapi.params import Form

app = ALBResolver(enable_validation=True)

@app.post("/login")
def login(username: Annotated[str, Form()]):
    return {"username": username}

base = {"requestContext": {"elb": {"targetGroupArn": "arn"}}, "httpMethod": "POST", "path": "/login", "isBase64Encoded": False, "body": "username=khaled"}

single = {**base, "headers": {"content-type": "application/x-www-form-urlencoded"}}
multi = {**base, "multiValueHeaders": {"content-type": ["application/x-www-form-urlencoded"]}}

print(app.resolve(single, {})["statusCode"])  # 200
print(app.resolve(multi, {})["statusCode"])   # 422  <- expected 200

Possible Solution

In _get_content_type, keep reading headers first and fall back to resolved_headers_field when it has no Content-Type:

event = app.current_event
content_type = event.headers.get("content-type") or event.resolved_headers_field.get("content-type", "")

Events that send headers (API Gateway REST/HTTP, single-value ALB, VPC Lattice) keep the current behaviour; only the case where headers is empty changes. The existing list handling already covers multi-value values. (Reading resolved_headers_field first instead would change behaviour for API Gateway REST, which sends both fields, so I kept headers first.)

Branch with the fix and a functional test using albMultiValueHeadersEvent.json (both content-type and Content-Type keys): https://github.com/khaledix3/powertools-lambda-python/tree/fix/event-handler-multi-value-content-type

The new test fails on develop and passes with the fix; tests/functional/event_handler and tests/unit have no new failures. Happy to open the PR once this is triaged.

This is similar in shape to #6912 (compression with multiValueHeaders). Request.headers (event_handler/request.py) and the correlation-id middleware also read event.headers; I left those out of scope.

I used an AI assistant (Claude) to find and fix this; the snippet and tests were run, not just derived from reading the code.

Steps to Reproduce

  1. Run the snippet above on develop (0bbc252) or the latest release.
  2. The single-value request returns 200, the multi-value one returns 422 with json_invalid.

Did you run the reproduction yourself?

Yes, I ran it and saw the failure

Powertools for AWS Lambda (Python) version

latest (3.41.0, also develop 0bbc252)

AWS Lambda function runtime

3.13

Packaging format used

PyPi

Debugging logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtriage

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions