21

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?

5 Answers 5

25

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.

Sign up to request clarification or add additional context in comments.

2 Comments

How do I pass the request to the rails app with the []? thats the only way rails will take it
This is the right answer in regards to Lambda and API Gateway.
11

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

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

5 Comments

Yes, this is classic JSON and that's what API Gateway parses.
@napalm The problem is that there are lots of frameworks using conventions like this; columns[0][data]=id&columns[0][name]=ID&columns[0][searchable]=true!
You should be able to pass and parse the URL fragment in your Lambda.
How would a #foreach on colors look like in this case?
Is there a chance ta pass this list using data mapping template when i use aws type integration to lambda? lambda gets only last value from list...
8

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"]

1 Comment

I found adding indicies to be simple and effective, thanks for the suggestion.
4

If anyone is using OpenAPI 3.0 spec in AWS Api Gateway then you need to define this in your x-amazon-apigateway-integration like so.

x-amazon-apigateway-integration:
        ...
        requestParameters:
            integration.request.querystring.colors: method.request.multivaluequerystring.colors
        ...

Comments

3

As discussed in @kjs3 answer, API Gateway doesn't support duplicate parameters in the query string or headers. However you can use the single parameter array format.

Supporting duplicate parameter keys+values is something we do want to do, but I don't have an ETA at the moment.

4 Comments

Hello Jack - Thank you for the info! Do you have any new information at the moment?
Should have support soon.
Hi @Jack do you have a view on how soon?
I can see this limitation has been removed from the known issues. Is it still the case? From what I can see only the last occurrence of same-named query params is passed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.