【问题标题】:Testing aws lambda python function with nested json使用嵌套 json 测试 aws lambda python 函数
【发布时间】:2021-09-28 03:06:55
【问题描述】:

以下是我想作为测试事件输入的内容,也是我的 lambda python 函数知道如何处理的内容。

{
  "body": '{"email":"example@example.com","password":"Example1"}',
  "path": "/SignIn"
}

但是,您不能将其配置为 aws lambda 中的测试事件,因为您会收到以下 - 有点合理 - 错误:

There is an error in your JSON event. Please correct it before saving.

这是有道理的,因为 JSON 中不应该有任何单引号。但是,如果我也使用所有双引号,它也不起作用。

有什么想法吗?漏洞?

【问题讨论】:

  • JSON 字符串必须使用双引号作为分隔符。与“body”关联的值应该是字符串还是嵌套对象?
  • 如果必须是字符串,请尝试"{\"email\":\"example@example.com\",\"password\":\"Example1\"}"。否则,只需去掉单引号。
  • 太棒了!谢谢@PM2Ring,这正是我所需要的!

标签: python json amazon-web-services aws-lambda


【解决方案1】:

你可以这样做

{
  "body": "{\"email\":\"example@example.com\",\"password\":\"Example1\"}",
  "path": "SignIn"
}

然后执行 json.loads() 并摆脱“”,但理想情况下我永远不会像这样在 API 有效负载上发送密码!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多