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

Posted

技术标签:

【中文标题】JSON 有效负载、AWS Lambda (Python)、API 网关【英文标题】:JSON Payload, AWS Lambda (Python), API Gateway 【发布时间】:2021-07-24 03:17:33 【问题描述】:

我正在通过 API Gateway 接收到我的 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)。但他们都以相同的结果结束。

【问题讨论】:

【参考方案1】:

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

import ast

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

那么您应该可以将body 用作常规地图。

替代方案

import json

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

【讨论】:

感谢您的回复。不幸的是,我现在收到此错误; ValueError:错误的节点或字符串: @AAA 是什么产生了身体?是 AWS 服务,还是一些第三方应用程序? 它是一个名为 OnFleet 的第三方工具生成有效负载,它调用我们在 AWS 上的 Web 挂钩(API Gateway 然后我们的 Lambda (Python) 函数。 @AAA 我提供了替代方案。你能检查一下吗?

以上是关于JSON 有效负载、AWS Lambda (Python)、API 网关的主要内容,如果未能解决你的问题,请参考以下文章

如何从 JAVA 调用 AWS lambda 函数?

如何正确地将复杂的 Swift 对象序列化/反序列化为 AWS Lambda 有效负载?

AWS Lambda 函数 - 无法调用更新事物影子

Aws Lambda 响应错误

aws lambda函数调用内部负载均衡器

通过 AWS SNS 将 JSON 数据负载发送到 FCM