从 Lambda 事件中提取内容。

huangapple go评论76阅读模式
英文:

Extract content from lambda event

问题

你可以使用以下代码来提取通过API Gateway和Postman发送的JSON并使用json.loads加载它:

import json

def lambda_handler(event, context):
    # 获取POST请求的body
    body = event['body']

    # 将body解析为JSON
    posted_json = json.loads(body)

    # 现在你可以访问posted_json中的数据
    invoiceId = posted_json['data']['invoice']['invoiceId']

这将允许你从Postman发送的JSON中提取数据并将其加载为Python字典。

英文:

I have a lambda with API gateway. So far I have tested it via "Test" tab in aws console and using below json file

{
  "datasource": "XXX",
  "action": "update",
  "code": "test",
  "time": "2023-03-20T11:39:50.0038136+00:00",
  "data": {
    "invoice": {
      "invoiceId": "xxxx58d752f6f7f5d",
      "type": "fixed",
      "value": 5000
    }
  }
}

In my lambda

def lambda_handler(event, context):
    s1 = json.dumps(event)
    inv_event = json.loads(s1)
    # Im reading values like below
    invoiceId = inv_event['data']['invoice']['invoiceId']

This is working fine as expected. But now I am trying to hit this via api gateway and post the same json via postman and this started failing with error "[ERROR] TypeError: string indices must be integers". I have done a print of incoming event when call via postman and its like below

{'resource': '/hello', 'path': '/hello', 'httpMethod': 'POST', 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br', 'CloudFront-Forwarded-Proto': 'https', 'CloudFront-Is-Desktop-Viewer': 'true', 'CloudFront-Is-Mobile-Viewer': 'false', 'requestContext': {'resourceId': '3mm431', 'resourcePath': '/hello', 'httpMethod': 'POST', 'extendedRequestId': 'DoIXeECnIAMF8eg=', 'requestTime': '19/Apr/2023:13:34:01 +0000', 'path': '/Prod/hello', 'accountId': '597878062269', 'protocol': 'HTTP/1.1', 'stage': 'Prod', 'domainPrefix': 'xxxx', 'requestTimeEpoch': 1681911241190, 'requestId': 'ba1ee0c7-8b57-42bc-9335-d5548b1a084a', 'identity': {'cognitoIdentityPoolId': None, 'accountId': None, 'cognitoIdentityId': None, 'caller': None, 'sourceIp': '86.139.218.222', 'principalOrgId': None, 'accessKey': None, 'cognitoAuthenticationType': None, 'cognitoAuthenticationProvider': None, 'userArn': None, 'userAgent': 'PostmanRuntime/7.32.2', 'user': None}, 'domainName': 'xxxx.execute-api.xxxx.amazonaws.com', 'apiId': 'xxxx'}, 'body': '{\r\n      \"datasource\": \"XXX\",\r\n      \"action\": \"update\",\r\n      \"code\": \"test\",\r\n      \"time\": \"2023-03-20T11:39:50.0038136+00:00\",\r\n      \"data\": {\r\n        \"invoice\": {\r\n          \"invoiceId\": \"xxxx58d752f6f7f5d\",\r\n          \"type\": \"fixed\",\r\n          \"value\": 5000\r\n        }\r\n      }\r\n    }', 'isBase64Encoded': False}

Is there a way for me to extract the posted json and load it as json.loads?

答案1

得分: 1

You're just converting the entire event from a dict to a JSON string, and back to a dict with these two lines, they aren't actually accomplishing anything:

只是将整个事件从字典转换为JSON字符串,然后再转换回字典,这两行代码实际上没有完成任何任务:

s1 = json.dumps(event)
inv_event = json.loads(s1)


The body attribute of the event is a string representation of the request payload that was sent to API Gateway. You need to tell Python that that specific string is JSON, and needs to be parsed into a dict.

事件的body属性是发送到API Gateway的请求负载的字符串表示形式。您需要告诉Python该字符串是JSON,并需要解析为字典。

inv_event = json.loads(event['body'])
invoiceId = inv_event['data']['invoice']['invoiceId']


<details>
<summary>英文:</summary>

You&#39;re just converting the entire event from a dict to a JSON string, and back to a dict with these two lines, they aren&#39;t actually accomplishing anything:

s1 = json.dumps(event)
inv_event = json.loads(s1)

The `body` attribute of the event is a string representation of the request payload that was sent to API Gateway. You need to tell Python that that specific string is JSON, and needs to be parsed into a dict.

inv_event = json.loads(event['body'])
invoiceId = inv_event['data']['invoice']['invoiceId']


</details>



huangapple
  • 本文由 发表于 2023年4月19日 21:53:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定