Home > Software engineering >  How to query parameters with API Gateway to lambda
How to query parameters with API Gateway to lambda

Time:05-30

I made a REST API with AWS Lambda API Gateway.

my API Gateway's Integration Request is LAMBDA_PROXY Type,

and I use params in Lambda like this. ( myparam is list type)

def lambda_handler(event, context):
    # TODO implement
    try:
        myparam = event['multiValueQueryStringParameters']['param1']
    #...

I tested my REST API in python like this.

url = 'https://***.amazonaws.com/default/myAPI'

param = {'param1':['1','2']}

res = requests.get(url=url,params=param).json()
print(res)

It works. but when I tried with another way like this,

url = 'https://***.amazonaws.com/default/myAPI?param1=1,2'

res = requests.get(url=url).json()
print(res)

It didn't work with this way. How to query parameters in case if I want to insert parameter into url directly?

CodePudding user response:

Those tow requests are not equivalent. In order to prove it, we can print the formatted URL for the first request:

url = 'https://***.amazonaws.com/default/myAPI'

param = {'param1':['1','2']}

res = requests.get(url=url,params=param).json()

# Print the request URL
print(res.request.url)

This will print something like:

https://***.amazonaws.com/myAPI?param1=1&param1=2

So, in your second snippet, you probably would want to create your URL as follows:

url = 'https://***.amazonaws.com/myAPI?param1=1&param1=2'

res = requests.get(url=url).json()
print(res)

If you want to separate your parameters with commas, the value for param1 will be a string ('1,2'), not an list.

  • Related