5

I am trying to return two cookies from Lambda to client(Postman) via APIGateway. I referred to the aws blog, and could return 1 cookie to client.

https://aws.amazon.com/jp/blogs/compute/simply-serverless-using-aws-lambda-to-expose-custom-cookies-with-api-gateway/

But I have no idea to return 2 kind of cookies. I tried like this, but not working.

return {
   'cookie': 'aaa=bbb; secure'               -> Okay, but just one cookie
    # 'cookie': 'aaa=bbb; ccc=ddd; secure'   -> ×
    # 'cookie': ['aaa=bbb','ccc=ddd']        -> ×
}

enter image description here Could anyone give me an advise?

yagu
  • 177
  • 1
  • 9
  • Possible duplicate of [How can I send multiple Set-Cookie headers from API Gateway using a proxied Lambda](https://stackoverflow.com/questions/39769222/how-can-i-send-multiple-set-cookie-headers-from-api-gateway-using-a-proxied-lamb) – dege Apr 23 '18 at 10:43

3 Answers3

9

As far as I can tell, you can't set multiple cookies in the same Set-Cookie header. There's literature on the internet saying that you can, but my attempts to replicate this in API Gateway were fruitless.

Additionally, at the time of writing, API Gateway doesn't allow you to set multiple Set-Cookie headers. This has been a long-requested and still not implemented feature.

If you can, I'd suggest packing all your information into a single object and sending the JSON in one cookie.


If you really need to though, here is a disgusting workaround...

API Gateway's response headers are case-sensitive, so you can define multiple instances of the set-cookie header by varying the case, like this:

Set-Cookie
SET-COOKIE
SeT-CoOkIe
Nicholas Sizer
  • 3,490
  • 3
  • 26
  • 29
  • I despise the case sensitive hack, but at the same time I can not stop laughing. – Rab Feb 01 '23 at 23:23
1

With the Lambda function response for format 2.0, as stated in the official documentation, to customize the response, your Lambda function should return a response with the following format.

{
    "cookies" : ["cookie1", "cookie2"],
    "isBase64Encoded": true|false,
    "statusCode": httpStatusCode,
    "headers": { "headername": "headervalue", ... },
    "body": "Hello from Lambda!"
}    
Camilo Silva
  • 8,283
  • 4
  • 41
  • 61
  • 1
    Looks like you can use the `"cookies"` field on lambda responses instead of `Set-Cookie` headers. Much better than the current top answer of using case changing. – Preston Hager Feb 28 '23 at 02:59
0

Setup mapping with Response header Set-Cookie and mapping value “integration.response.multivalueheader.Set-Cookie”

source "https://medium.com/nirman-tech-blog/aws-gateway-api-endpoints-multi-value-cookie-mapping-a36586cebd5e"

Alex Diaz
  • 55
  • 9