Closed XDanny322 closed 5 years ago
Sorry, i meant to create this issue in this repo: https://github.com/awslabs/serverless-application-model
Closing this issue.
having the same problem, shame I can't find the equivalent issue in https://github.com/awslabs/serverless-application-model @XDanny322
This issue appears to be by design - sam local start-api
starts up a local API gateway and HTTP requests get constructed into API Gateway Lambda Events (https://github.com/aws/aws-sam-cli/blob/develop/samcli/local/apigw/event_constructor.py), which as @XDanny322 noted, takes the HTTP request body (amongst other fields) and puts that into the corresponding body
field on the event. Coming at this from a non-API perspective, this wasn't obvious behaviour and not something that seems documented - all the start-api
docs I've seen refer to GET
requests without a request body.
In short, any lambda functions that are configured for API Events
are expected to receive API Gateway Events, which makes sense in hindsight. This means that in order to call such a function otherwise (e.g. with sam local invoke
) you would need to create the expected gateway event structure. Or, conversely, if a given function isn't intended for API event handling, then skip using sam local start-api
, remove the Events
handler, and just use sam local invoke
to directly call the function instead.
Description
In short - when running
sam local start-api
, POST some data to the local endpoint so that the data can be sent to a Lambda.. it seems like the data gets "massaged" and are "pushed" under a key called "body".In long.. I am using sam to create some simple resources. A Lambda Function, and a API Gateway, which i am declaring directly by calling "AWS::Serverless::Api" and not inferring it from an "Event" within a function. Relevant parts of the SAM template below.
When i
sam build
.......and
sam local start-api
all is well.However, when i curl post to it and send in some data, it seems like the APIGateway is massaging the data.
The POST, note how the data is only one key value pair of
{"id": "61763eb7-c980-4ef1-a58d-04bd8b8caf14"}
sam local's APIGatway response:
I have tested the post from curl, and postman, and its the same; that it causes this problem. I have also tested against a real APIGatway running in AWS, and the problem does not happen.
The point here is, when i am posting this data:
sam local APIGateway seems to massage the data, puts MY data under a key called "body".. add other header, etc stuff, and then pass it to my lambda.
Steps to reproduce
See description
Observed result
sam local start-api
seems to massage data before sending to lambda.Expected result
The data i post from curl / postman, gets sent as is, to the lambda.
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)