- Mais recentes
- Mais votos
- Mais comentários
Hey Pier, you are right. My answer was wrong in your context, sorry about that. But the body being string is by design as noted by Uri.
This is by design.
I think you are only able to change it using API Gateway. Lambda functions with PROXY integration accelerates your integration process as you don't need to create the request and response templates. And, as the Lambda can receive binary payload in BASE64, the body
should have a type for this case also, as, the payload is not JSON.
You can still receive the payload as JSON if you setup the request and response templates by yourself.
When calling the function from outside, you'll get an event composed by the system, and in it, the body will be a string. If you want to use it as an object, you need to convert it back. For example, in node.js, you need to use something like this:
exports.handler = async (event) => {
const body = JSON.parse(event.body)
...
ps. You may also want to prepare for errors in the body, as it may be invalid, so, you can put the parse in a try..catch block, and check if the required parameter is defined etc.
I haven't found any documentation of this in the AWS Lambda documentation. It seems like a basic fundamental concern that should be addressed in AWS Lambda 101. Did I miss the documentation or do you have to trawl through user questions to get this information?
p.s. I've found some related documentation under AWS API Gateway, but I still don't see Lambda documentation of fundamental concerns such as parsing of the JSON body, HTTP status header control for responses, additional content that Lambda seems to generate in the response, etc.: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-integrations.html
Thank you for your answer, Eduardo.
How can set up a request template for a Lambda URL? I haven't seen that being supported for Lambda URLs. The link you posted is for API Gateway.
The reason provided by Eduardo is correct, although the details relate to API Gateway with Lambda and not Lambda Function URLs.
I think that depending on the programming language, you should be able to parse it easily, e.g., in Python use something like
body = json.loads(event.body)