The function is for a SAM template that looks something like the following:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Example Template for AWS HTTP API to Python Lambda Function
Parameters:
StageNameParameter:
Type: String
Description: The API Gateway Stage Name
Default: sandbox
Resources:
HttpApi:
Type: AWS::Serverless::HttpApi
Properties:
StageName: !Ref StageNameParameter
Tags:
Tag: Value
AccessLogSettings:
DestinationArn: !GetAtt HttpApiAccessLogs.Arn
Format: $context.stage $context.integrationErrorMessage $context.identity.sourceIp $context.identity.caller $context.identity.user [$context.requestTime] "$context.httpMethod $context.resourcePath $context.protocol" $context.status $context.responseLength $context.requestId $context.extendedRequestId
DefaultRouteSettings:
ThrottlingBurstLimit: 200
RouteSettings:
"POST /example":
ThrottlingBurstLimit: 500 # overridden in HttpApi Event
StageVariables:
StageVar: Value
FailOnWarnings: true
HttpApiAccessLogs:
Type: AWS::Logs::LogGroup
Properties:
RetentionInDays: 90
ApiFunctionLogs:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${ApiFunction}
RetentionInDays: 7
ApiFunction: # Adds a GET api endpoint at "/" to the ApiGatewayApi via an Api event
Type: AWS::Serverless::Function
Properties:
Events:
ExplicitApi: # warning: creates a public endpoint
Type: HttpApi
Properties:
ApiId: !Ref HttpApi
Method: POST
Path: /example
TimeoutInMillis: 30000
PayloadFormatVersion: "2.0"
RouteSettings:
ThrottlingBurstLimit: 600
Runtime: python3.8
Handler: function.handler
CodeUri: path/to/src
One of two requests can now be made that will both be parsed correctly: first with JSON payload and the second with FORM DATA:
curl -d '{"Message": "Test123"}' -H "Content-Type: application/json" -X POST https://nnnnnnnnnn.execute-api.eu-central-1.amazonaws.com/sandbox/example
curl -d "param1=value1¶m2=value2" -X POST https://nnnnnnnnnn.execute-api.eu-central-1.amazonaws.com/sandbox/example