Serverless Framework Python Lambda Return Json Directly
I'm trying to find out how can I return the response as a JSON directly using Serverless Framework. This is a function on AWS with Lambda Proxy Integration. All default setting. Th
Solution 1:
approach #1
Use json.dumps() to convert JSON to string.
import json
defhandle(event, context):
log.info("Hello Wold")
log.info(json.dumps(event, indent=2))
return {
"statusCode": 200,
"body": json.dumps({"foo": "bar"}),
"headers": {
"Content-Type": "application/json"
}
}
approach #2
Use lambda integration and avoid json.dumps(). But this will transform your output as
{ foo = bar}
Solution 2:
The body needs to be stringified when working with API Gateway
The pythonish
way to do it is to pass the JSON body into the json.dumps
function.
defhandle(event, context):
log.info("Hello Wold")
log.info(json.dumps(event, indent=2))
return {
"statusCode": 200,
"body": json.dumps({"foo": "bar"}),
"headers": {
"Content-Type": "application/json"
}
}
Post a Comment for "Serverless Framework Python Lambda Return Json Directly"