HUWWW
这是因为当event对象来自 API Gateway 时,它上面有一些额外的信息。它不像您用来从控制台进行测试的 JSON 那样简单。您需要首先访问该body对象,然后最后访问您的 JSON 对象。以下是来自 API Gateway 的事件的样子:{ "path": "/test/hello", "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "Accept-Encoding": "gzip, deflate, lzma, sdch, br", "Accept-Language": "en-US,en;q=0.8", "CloudFront-Forwarded-Proto": "https", "CloudFront-Is-Desktop-Viewer": "true", "CloudFront-Is-Mobile-Viewer": "false", "CloudFront-Is-SmartTV-Viewer": "false", "CloudFront-Is-Tablet-Viewer": "false", "CloudFront-Viewer-Country": "US", "Host": "wt6mne2s9k.execute-api.us-west-2.amazonaws.com", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48", "Via": "1.1 fb7cca60f0ecd82ce07790c9c5eef16c.cloudfront.net (CloudFront)", "X-Amz-Cf-Id": "nBsWBOrSHMgnaROZJK1wGCZ9PcRcSpq_oSXZNQwQ10OTZL4cimZo3g==", "X-Forwarded-For": "192.168.100.1, 192.168.1.1", "X-Forwarded-Port": "443", "X-Forwarded-Proto": "https" }, "pathParameters": { "proxy": "hello" }, "requestContext": { "accountId": "123456789012", "resourceId": "us4z18", "stage": "test", "requestId": "41b45ea3-70b5-11e6-b7bd-69b5aaebc7d9", "identity": { "cognitoIdentityPoolId": "", "accountId": "", "cognitoIdentityId": "", "caller": "", "apiKey": "", "sourceIp": "192.168.100.1", "cognitoAuthenticationType": "", "cognitoAuthenticationProvider": "", "userArn": "", "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48", "user": "" }, "resourcePath": "/{proxy+}", "httpMethod": "GET", "apiId": "wt6mne2s9k" }, "resource": "/{proxy+}", "httpMethod": "GET", "queryStringParameters": { "name": "me" }, "stageVariables": { "stageVarName": "stageVarValue" }, "body": "'{\"user\":\"john\",\"pwd\":\"pwd1\"}'" }请记住,body来自 API 网关的始终是字符串化的,因此如果您想访问它,首先需要使用json.loads(event["body"]).请记住,当返回 API Gateway 时,您的响应正文必须是字符串化的,正如我们在此答案中所讨论的那样。您可以在文档中看到从 API Gateway 发送的事件
慕神8447489
我从回复中得到的关键与他的建议不同,但他的建议对我有帮助,所以我接受它作为答案我得到了这个回应。该body键来为null。但有queryStringParameters{ "resource": "/match_creds", "path": "/match_creds", "httpMethod": "GET", "headers": null, "multiValueHeaders": null, "queryStringParameters": { "pwd": "pwd1", "user": "JOHN" }, "multiValueQueryStringParameters": { "pwd": [ "pwd1" ], "user": [ "JOHN" ] }, "pathParameters": null, "stageVariables": null, "requestContext": { "path": "/match_creds", "accountId": "", "resourceId": "", "stage": "test-invoke-stage", "domainPrefix": "testPrefix", "requestId": "", "identity": { "cognitoIdentityPoolId": null, "cognitoIdentityId": null, "apiKey": "test-invoke-api-key", "cognitoAuthenticationType": null, "userArn": "", "apiKeyId": "test-invoke-api-key-id", "userAgent": "", "accountId": "", "caller": "", "sourceIp": "test-invoke-source-ip", "accessKey": "", "cognitoAuthenticationProvider": null, "user": "" }, "domainName": "testPrefix.testDomainName", "resourcePath": "/match_creds", "httpMethod": "GET", "extendedRequestId": "", "apiId": "" }, "body": null, "isBase64Encoded": false}我改变了我的功能import json def lambda_handler(event, context): json_data = event["queryStringParameters"] user = json_data["user"] return { "statusCode": 200, "headers": {"Content-Type": "application/json"}, "body": json.dumps(user) }