JSON 负载、AWS Lambda (Python)、API 网关

JSON Payload, AWS Lambda (Python), API Gateway

我正在通过 API 网关接收负载到我的 Lambda 函数。

{
"resource": "/onfleet-webhook-production",
"path": "/onfleet-webhook-production",
"httpMethod": "POST",
"headers": {
    "accept": "application/json",
    "content-type": "application/json",
    "Host": "fje4zhwf83.execute1-api.us-east-1.amazonaws.com",
    "X-Amzn-Trace-Id": "Root=1-60884247-298cc6854b722b00668b0553",
    "X-Forwarded-For": "54.84.188.171",
    "X-Forwarded-Port": "443",
    "X-Forwarded-Proto": "https",
    "X-Onfleet-Signature": "539370210b482cbb4f7d728ecb33a690c89ff70dc574475000788d0fedfdd25471a45e909f42a951d8ccbb53c22ab0a548f3c7a7fa965b697ab13544c92ff08b"
},
"multiValueHeaders": {
    "accept": ["application/json"],
    "content-type": ["application/json"],
    "Host": ["fje4zhwf83.execute-api.us-east-1.amazonaws.com"],
    "X-Amzn-Trace-Id": ["Root=1-60884247-298cc6854b722b00668b0553"],
    "X-Forwarded-For": ["54.84.188.171"],
    "X-Forwarded-Port": ["443"],
    "X-Forwarded-Proto": ["https"],
    "X-Onfleet-Signature": ["539370210b482cbb4f7d728ecb33a690c89ff70dc574475000788d0fedfdd25471a45e909f42a951d8ccbb53c22ab0a548f3c7a7fa965b697ab13544c92ff08b"]
},
"queryStringParameters": null,
"multiValueQueryStringParameters": null,
"pathParameters": null,
"stageVariables": null,
"requestContext": {
    "resourceId": "0xos2v",
    "resourcePath": "/onfleet-webhook-production",
    "httpMethod": "POST",
    "extendedRequestId": "ec9LLEw0oAMFkwA=",
    "requestTime": "27/Apr/2021:16:56:39 +0000",
    "path": "/default/onfleet-webhook-production",
    "accountId": "406135884221",
    "protocol": "HTTP/1.1",
    "stage": "default",
    "domainPrefix": "fje4zhwf83",
    "requestTimeEpoch": 1619542599421,
    "requestId": "9485a44a-44f7-436c-9e98-0cb7d9034513",
    "identity": {
        "cognitoIdentityPoolId": null,
        "accountId": null,
        "cognitoIdentityId": null,
        "caller": null,
        "sourceIp": "54.84.188.171",
        "principalOrgId": null,
        "accessKey": null,
        "cognitoAuthenticationType": null,
        "cognitoAuthenticationProvider": null,
        "userArn": null,
        "userAgent": null,
        "user": null
    },
    "domainName": "fje4zhwf83.execut1e-api.us-east-1.amazonaws.com",
    "apiId": "fje4zhwf83"
},
"body": "{\"taskId\":\"f7XbmRxXvfntB086PseFXpZ9\",\"actionContext\":{\"type\":\"WORKER\",\"id\":\"C~s63JVCJTtD12J4BCY2Y4cN\"},\"triggerId\":0,\"triggerName\":\"taskStarted\",\"workerId\":\"C~s63JVCJTtD12J4BCY2Y4cN\",\"adminId\":null,\"data\":{\"task\":{\"id\":\"f7XbmRxXvfntB086PseFXpZ9\",\"timeCreated\":1619535291000,\"timeLastModified\":1619535394486,\"organization\":\"hFz5MmFqcMoMwjYmvvPbBo5U\",\"shortId\":\"4334535c\",\"trackingURL\":\"https://onf.lt/4334535c0c\",\"worker\":\"C~s63JVCJTtD12J4BCY2Y4cN\",\"merchant\":\"hFz5MmFqcMoMwjYmvvPbBo5U\",\"executor\":\"hFz5MmFqcMoMwjYmvvPbBo5U\",\"creator\":\"YG5INuyrKz4dggsW7Vo0q*Nv\",\"dependencies\":[],\"state\":2,\"completeAfter\":null,\"completeBefore\":null,\"pickupTask\":false,\"notes\":\"\",\"completionDetails\":{\"failureNotes\":\"\",\"failureReason\":\"NONE\",\"events\":[],\"actions\":[],\"time\":null,\"firstLocation\":[],\"lastLocation\":[],\"unavailableAttachments\":[]},\"feedback\":[],\"metadata\":[],\"overrides\":{},\"quantity\":0,\"serviceTime\":0,\"identity\":{\"failedScanCount\":0,\"checksum\":null},\"appearance\":{\"triangleColor\":null},\"container\":{\"type\":\"WORKER\",\"worker\":\"C~s63JVCJTtD12J4BCY2Y4cN\"},\"trackingViewed\":false,\"estimatedCompletionTime\":null,\"estimatedArrivalTime\":null,\"destination\":{\"id\":\"yIDUKM65Ck8bwNEP8z4W42Ea\",\"timeCreated\":1619535291000,\"timeLastModified\":1619535291451,\"location\":[-71.25429253636486,42.244868727926736],\"address\":{\"apartment\":\"\",\"state\":\"Massachusetts\",\"postalCode\":\"02030\",\"number\":\"35\",\"street\":\"Strawberry Hill Street\",\"city\":\"Dover\",\"country\":\"United States\"},\"notes\":\"\",\"metadata\":[],\"googlePlaceId\":null,\"warnings\":[]},\"recipients\":[{\"id\":\"5DoYN3*udPr8eUuEELGPq6yk\",\"timeCreated\":1571336170000,\"timeLastModified\":1619535291465,\"name\":\"s Test\",\"phone\":\"+16378343616\",\"notes\":\"Testing Email\",\"organization\":\"hFz5MmFqcMoMwjYmvvPbBo5U\",\"skipSMSNotifications\":false,\"metadata\":[]}]}},\"time\":1619535394615}",
"isBase64Encoded": false

}

我可以访问此 JSON 中的值; 前任 。 data = event.get('path')

但是,我无法访问 'body'

中的任何变量
`event.get('body')` doesn't seem to work at all

如何访问正文中的值,如 'taskId'、“timeCreated”等。

我尝试过不同的东西,比如 json.loads(event)。但他们最终都得到了相同的结果。

你的body是一个字符串。您必须使用 ast 将其解析为有效的 python 映射:

import ast

body = ast.literal_eval(event['body'])

那么你应该可以使用body作为普通地图。

备选

import json

body = json.loads("{}".format(event['body']))