Python3.7 With proxy integration
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.
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)
}
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.
Let's take the method that suits the purpose according to the parameters passed.
Recommended Posts