JWB
JWB

Reputation: 198

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. 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:

{
"message":"Internal server error"
}

When I test the lambda function it returns the error:

{
  "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?

Upvotes: 7

Views: 40606

Answers (5)

alexdabest
alexdabest

Reputation: 71

Make sure you select the checkbox 'Use Lambda Proxy integration' at the point of choosing your Lambda function integration.

Upvotes: 1

ballb
ballb

Reputation: 1

It's because of the typo in responseObject['StatusCode'] = 200.

'StatusCode' should be 'statusCode'.

I got the same issue, and it was that.

Upvotes: -1

Joshua Dixon
Joshua Dixon

Reputation: 20

The problem is not your code. It is the Lambda function intergration setting. Please do not enable Lambda function intergration setting . You can still attach the Lambda function without it. Leave this unchecked.

Upvotes: 0

Gordon Christie
Gordon Christie

Reputation: 39

just remove ['queryStringParameters']. the print event line shows the event i only a array not a key value pair. I happen to be following the same tutorial. I'm still on the api gateway part so i'll update once mine is completed.

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.

Upvotes: 3

jarmod
jarmod

Reputation: 78890

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. 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).

In your case, it turns out that your test event included transactionId when your code expected to see transactionid (different spelling). Hence the KeyError exception.

Upvotes: 11

Related Questions