Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass array query params to AWS API Gateway?

A Rails API typically likes array query params like this:

example.com?colors[]=cyan&colors[]=magenta&colors[]=yellow&colors[]=black

How would I map that through to a lambda function?

like image 239
kjs3 Avatar asked Aug 05 '15 22:08

kjs3


People also ask

How do I add a parameter to API gateway?

Open the AWS Api Gateway console and click on your API's name. In the Resources tab, click on the specific HTTP method. Click on Method Request and expand the URL Query String Parameters section. Click on Add query string and type in the name of your query string parameter.

What is multiValueHeaders?

The multiValueHeaders header is translated to multiple Set-Cookie headers by API Gateway and appears to the API client as the following: Set-Cookie →language=en-US.


3 Answers

Multiple arguments with same name in query string are now supported in API Gateway proxies. They are passed in the multiValueQueryStringParameters dictionary.

E.g.

 GET /api/path/?param=value&param=othervalue&something=thing

Will generate the following request:

{
    "resource": "/{proxy+}",
    "path": "/ap/path/",
    "httpMethod": "GET",
    "queryStringParameters": {
        "param": "othervalue",  # only the last value is kept here
        "something": "thing"
    },
    "multiValueQueryStringParameters": {
        "param": [
            "value",
            "othervalue"
        ],
        "something": [
            "thing"
        ]
    },
    "pathParameters": {
        "proxy": "api/path"
    },
    # etc
}

See documentation.

like image 150
Antwan Avatar answered Sep 22 '22 16:09

Antwan


A little trial and error shows that it's like this:

example.com?colors=['cyan','magenta','yellow','black']

like image 26
kjs3 Avatar answered Sep 20 '22 16:09

kjs3


One thing you can't do is have duplicated query string param keys as per https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html:

Duplicated headers are not supported.

API Gateway maps the parameters to a JSON object so as long as each item has their own unique key, you won't get any clobbering.

Your option is good but I brainstormed some other options. You could do something like adding indicies in the square brackets:

example.com?colors[0]=cyan&colors[1]=magenta&colors[2]=yellow&colors[3]=black

You could send the query string in the POST body if you were willing to swap from GET to POST. Then you can parse the raw query string yourself. You can even have repeated param keys here because you are parsing it.

?colors=cyan&colors=magenta&colors=yellow&colors=black

Another POST option is to send a JSON array in the POST body. I know POST isn't as easy to work with as GET but it's an option.

["cyan","magenta","yellow","black"]
like image 40
Tom Saleeba Avatar answered Sep 22 '22 16:09

Tom Saleeba