【发布时间】:2021-01-13 03:23:20
【问题描述】:
我有一个托管在 aws 上的 python 函数,它接受一个事件,然后做一堆事情,但本质上问题在于解析传入的 POST 请求。
我的代码如下所示:
import json
def lambda_handler(event, context):
# TODO implement
merchantid = json.dumps(event['body'])
return {
'statusCode': 200,
'body': json.dumps(merchantid)
}
为了发送 post 请求,我只是使用网站附带的 aws API Gateway 测试,所以它不应该是任何奇怪的编码。
当我传入一个 JSON 对象时,我希望 Mercerid 字段存储一个 json 对象。
相反,它返回这个字符串:
"\"{\\n \\\"merchantEmail\\\": \\\"timcook@gmail.com\\\",\\n \\\"customerEmail\\\": \\\"billgates@gmail.com\\\",\\n \\\"merchantWallet\\\": \\\"mWallet\\\",\\n \\\"transactionTotal\\\": 1.00,\\n \\\"transactionDenomination\\\": \\\"AUD\\\",\\n \\\"customerCurrency\\\": \\\"EUR\\\",\\n \\\"merchantAccess\\\" : {\\n \\\"merchantKey\\\": \\\"key\\\",\\n \\\"merchantSecret\\\": \\\"secret\\\"\\n },\\n \\\"customerAccess\\\" : {\\n \\\"customerKey\\\": \\\"ckey\\\",\\n \\\"customerSecret\\\": \\\"csecret\\\"\\n }\\n}\""
我从未见过这样的字符串,而且我似乎无法让它返回 JSON 格式。
有人知道我如何将其返回到正文中提交的原始 JSON 格式吗?
我应该提到,lambda_handler 使用测试事件与 JSON 完美配合,只有在我开始尝试使用 API Gateway 触发它时,我才开始遇到这个问题。
编辑: 这是我作为 PUT 请求的主体传递的 JSON 对象:
{
"merchantEmail": "timcook@gmail.com",
"customerEmail": "billgates@gmail.com",
"merchantWallet": "mWallet",
"transactionTotal": 1.00,
"transactionDenomination": "AUD",
"customerCurrency": "EUR",
"merchantAccess" : {
"merchantKey": "key",
"merchantSecret": "secret"
},
"customerAccess" : {
"customerKey": "ckey",
"customerSecret": "csecret"
}
}
编辑: 在我附加 API 网关之前,我正在处理它
merchantid = event['merchantEmail']
但是一旦我将它作为 PUT 的主体传入,将返回 502 内部服务器错误
【问题讨论】:
-
为什么需要 json.dumps ?
-
能否请您发布您放入的 JSON 对象,以便我们可以尝试重复您所做的事情?
标签: python json rest aws-lambda aws-api-gateway