[PYTHON] Differences between queryStringParameters and multiValueQueryStringParameters in AWS Lambda

environment

Python3.7 With proxy integration

Use queryStringParameters

You can get the parameters from the payload passed from API Gateway to Lambda.

request https://XXXX/XXXX/XXXX?id=1&value=foo


response = event.get('queryStringParameters')

return {
    'statusCode': 200,
    'body': json.dumps(response)
}

result

{
    "id": "1"
    "value": "foo"
}

You can get the value by the parameter name.

request https://XXXX/XXXX/XXXX?id=1&value=foo


response = event.get('queryStringParameters').get('value')

return {
    'statusCode': 200,
    'body': json.dumps(response)
}

result

"foo"

It will be null if the specified parameter name does not exist.

Use multiValueQueryStringParameters

You can also get the parameters here, It is used when there are multiple (array) of the same parameter name.

request https://XXXX/XXXX/XXXX?id=1&value=foo&value=bar&value=baz


response = event["multiValueQueryStringParameters"].get("value")

return {
    'statusCode': 200,
    'body': json.dumps(response)
}

result

{
    "id": [
        "1"
    ],
    "value": [
        "foo",
        "bar",
        "baz"
    ]
}

You have to be careful When you specify a parameter name that does not exist If you try to get the contents of the array as it is, an error will occur.

request https://XXXX/XXXX/XXXX?id=1

This is ok (will be null)


response = event["multiValueQueryStringParameters"].get("value")

return {
    'statusCode': 200,
    'body': json.dumps(response)
}

This is an error Let's stop this way of taking.

response = event["multiValueQueryStringParameters"].get("value")[0]

return {
    'statusCode': 200,
    'body': json.dumps(response)
}

When retrieving multiple (array) same parameter names with queryStringParameters

request https://XXXX/XXXX/XXXX?id=1&value=foo&value=bar&value=baz


response = event.get('queryStringParameters')

return {
    'statusCode': 200,
    'body': json.dumps(response)
}

result

{
    "id": "1"
    "value": "baz"
}

The baz is retrieved, overwritten with the last value.

Summary

Let's take the method that suits the purpose according to the parameters passed.

Recommended Posts

Differences between queryStringParameters and multiValueQueryStringParameters in AWS Lambda
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
Differences in syntax between Python and Java
Differences in multithreading between Python and Jython
[Python] Scraping in AWS Lambda
Differences between Windows and Linux directories
Difference between list () and [] in Python
Differences between yum commands and APT commands
Differences between symbolic links and hard links
Write AWS Lambda function in Python
Differences between Python, stftime and strptime
Differences in identity, equivalence, and aliases
I compared Node.js and Python in creating thumbnails using AWS Lambda
Install pip in Serverless Framework and AWS Lambda with Python environment
difference between statements (statements) and expressions (expressions) in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Easy server monitoring with AWS Lambda (Python) and result notification in Slack
[AWS] Link Lambda and S3 with boto3
Matplotlib Basics / Differences between fig and axes
Differences between Numpy 1D array [x] and 2D array [x, 1]
Differences between Django's request.POST ['hoge'] and request.POST.get ('hoge')
Differences between Ruby and Python (basic syntax)
Summary of the differences between PHP and Python
[python] Difference between variables and self. Variables in class
About the difference between "==" and "is" in python
Differences and commonalities between dict, list, and tuple types
Easy connection between Raspberry Pi and AWS IoT
Amazon API Gateway and AWS Lambda Python version
Differences between glibc, musl libc and go resolvers
Differences in behavior between append () and "+ =" operators when adding data to a list in Python
I put Selenium and headless chrome in AWS lambda. (Notes under Win10 environment, etc.)
Dynamic HTML pages made with AWS Lambda and Python
Investigate the relationship between TensorFlow and Keras in transition
Create Amazon Linux with AWS EC2 and log in
Difference between Ruby and Python in terms of variables
I tried running TensorFlow in AWS Lambda environment: Preparation
Calculate Pose and Transform differences in Python with ROS
EP 3 Know the Differences Between bytes, str, and unicode
Mutual conversion between JSON and YAML / TOML in Python
Difference between return, return None, and no return description in Python
[Python] Retry process (Exponential Backoff) memo in AWS Lambda
Make ordinary tweets fleet-like with AWS Lambda and Python
Compare "relationship between log and infinity" in Gauche (0.9.4) and Python (3.5.1)
Differences between numpy and pandas methods for finding variance