Skip to content

Bug: Empty proxy response bodies turn HTTP 204, 205, and 304 into 500 #5699

Description

@svozza

Expected Behavior

Returning { statusCode: 204, body: '' } from an HTTP route should produce a 204 response with no body. The same should hold for status 205 and 304.

Current Behavior

The router returns 500 instead. handlerResultToWebResponse passes the empty string to the Web Response constructor, which rejects a non-null body for these statuses. The router catches that error and returns an internal-server-error response.

Confirmed on current main at 3919a3ce3af3ff9fc10d8f6332260cef063822ec through Router.resolve(): all nine combinations of statuses 204/205/304 and REST API v1/HTTP API v2/ALB return 500.

The nine corresponding controls with body omitted pass, as do three controls returning { statusCode: 200, body: '' }. The existing routing test file, with these regressions added, reports 9 failed and 212 passed.

Code snippet

import { Router } from '@aws-lambda-powertools/event-handler/http';
import type { Context } from 'aws-lambda';

const app = new Router();

app.get('/empty', () => ({
  statusCode: 204,
  body: '',
}));

export const handler = async (event: unknown, context: Context) =>
  app.resolve(event, context);

Steps to Reproduce

  1. Register the route above.
  2. Invoke it with a REST API v1, HTTP API v2, or ALB GET /empty event.
  3. Observe status 500 instead of 204.
  4. Repeat with status 205 or 304; both also return 500.
  5. Remove body: ''; the requested status is returned correctly.

Possible Solution

Normalize an empty proxy-result body to null before constructing a Web Response for statuses that prohibit a body. Add regression coverage through Router.resolve() for 204, 205, and 304 across all three integrations.

Workaround: omit the body property.

Powertools for AWS Lambda (TypeScript) version

2.35.0 in the source checkout at 3919a3ce3af3ff9fc10d8f6332260cef063822ec.

AWS Lambda function runtime

24.x — reproduced locally on Node.js v24.19.0; no Lambda deployment was used.

Packaging format used

npm

Execution logs

The regression assertions fail with:

AssertionError: expected 500 to be 204
AssertionError: expected 500 to be 205
AssertionError: expected 500 to be 304
Tests: 9 failed | 212 passed (221)

Activity

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

Metadata

Metadata

Assignees

Labels

pending-releaseThis item has been merged and will be released soon

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions