Skip to content

Bug: API Gateway envelopes assumes message body is always a JSON #3512

Closed
@dreamorosi

Description

@dreamorosi

Expected Behavior

When working with the ApiGatewayEnvelope and ApiGatewayV2Envelope I should be able to extract the message body regardless of whether it's a plain text string, base64-encoded string, or a JSON-stringified one.

Current Behavior

When the message body is not a JSON-stringified object the envelope throws a runtime error as it's unable to parse it with JSON.parse().

Code snippet

import { z } from 'zod';
import { ApiGatewayEnvelope } from '@aws-lambda-powertools/parser/envelopes';

export const handler = async (event) => {
  try {
    ApiGatewayEnvelope.parse(event, z.string());
  } catch (error) {
    console.error(error);
  }
}

Steps to Reproduce

Send a request with a body that is either plain text or an encoded binary.

Possible Solution

No response

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

22.x

Packaging format used

npm

Execution logs

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingcompletedThis item is complete and has been merged/shippedparserThis item relates to the Parser Utility

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions