简体   繁体   English

如何正确调用 AWS Lambda + API Gateway 的 queryStringParameters?

[英]How to correctly call queryStringParameters for AWS Lambda + API Gateway?

I'm following a tutorial on setting up AWS API Gateway with a Lambda Function to create a restful API.我正在学习使用 Lambda 函数设置 AWS API Gateway 以创建 Restful API 的教程。 I have the following code:我有以下代码:

import json

def lambda_handler(event, context):
    # 1. Parse query string parameters
    transactionId = event['queryStringParameters']['transactionid']
    transactionType = event['queryStringParameters']['type']
    transactionAmounts = event['queryStringParameters']['amount']

    # 2. Construct the body of the response object
    transactionResponse = {}
    # returning values originally passed in then add separate field at the bottom
    transactionResponse['transactionid'] = transactionId
    transactionResponse['type'] = transactionType
    transactionResponse['amount'] = transactionAmounts
    transactionResponse['message'] = 'hello from lambda land'

    # 3. Construct http response object
    responseObject = {}
    responseObject['StatusCode'] = 200
    responseObject['headers'] = {}
    responseObject['headers']['Content-Type'] = 'application/json'
    responseObject['body'] = json.dumps(transactionResponse)

    # 4. Return the response object
    return responseObject

When I link the API Gateway to this function and try to call it using query parameters I get the error:当我将 API 网关链接到此函数并尝试使用查询参数调用它时,出现错误:

{
"message":"Internal server error"
}

When I test the lambda function it returns the error:当我测试 lambda 函数时,它返回错误:

{
  "errorMessage": "'transactionid'",
  "errorType": "KeyError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 5, in lambda_handler\n    transactionId = event['queryStringParameters']['transactionid']\n"
  ]

Does anybody have any idea of what's going on here/how to get it to work?有没有人知道这里发生了什么/如何让它工作?

just remove ['queryStringParameters'].只需删除 ['queryStringParameters']。 the print event line shows the event i only a array not a key value pair.打印事件行显示事件 i 只是一个数组而不是键值对。 I happen to be following the same tutorial.我碰巧正在学习相同的教程。 I'm still on the api gateway part so i'll update once mine is completed.我还在 api 网关部分,所以我会在我的完成后更新。

Whan you test from the lambda function there is no queryStringParameters in the event but it is there when called from the api gateway, you can also test from the api gateway where queryStringParameters is required to get the values passed.当您从 lambda 函数测试时,事件中没有 queryStringParameters,但是从 api 网关调用时它就在那里,您还可以从 api 网关进行测试,其中需要 queryStringParameters 来获取传递的值。

I recommend adding a couple of diagnostics, as follows:我建议添加几个诊断,如下所示:

import json

def lambda_handler(event, context):
    print('event:', json.dumps(event))
    print('queryStringParameters:', json.dumps(event['queryStringParameters']))

    transactionId = event['queryStringParameters']['transactionid']
    transactionType = event['queryStringParameters']['type']
    transactionAmounts = event['queryStringParameters']['amount']
    // remainder of code ...

That way you can see what is in event and event['queryStringParameters'] to be sure that it matches what you expected to see.这样您就可以看到eventevent['queryStringParameters'] ,以确保它与您希望看到的内容相匹配。 These will be logged in CloudWatch Logs (and you can see them in the AWS Lambda console if you are testing events using the console).这些将记录在 CloudWatch Logs 中(如果您使用控制台测试事件,则可以在 AWS Lambda 控制台中看到它们)。

In your case, it turns out that your test event included transactionId when your code expected to see transactionid (different spelling).在您的情况下,当您的代码希望看到transactionid (不同的拼写)时,事实证明您的测试事件包含transactionId Hence the KeyError exception.因此 KeyError 异常。

The problem is not your code.问题不在于您的代码。 It is the Lambda function intergration setting.它是 Lambda 函数集成设置。 Please do not enable Lambda function intergration setting .请不要启用 Lambda 函数集成设置。 You can still attach the Lambda function without it.您仍然可以在没有它的情况下附加 Lambda 函数。 Leave this unchecked.不勾选此项。

It's because of the typo in responseObject['StatusCode'] = 200.这是因为responseObject['StatusCode'] = 200中的拼写错误

' StatusCode ' should be ' statusCode '. ' StatusCode ' 应该是' statusCode '。

I got the same issue, and it was that.我遇到了同样的问题,就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM