How can I send multiple Set-Cookie headers from API Gateway using a proxied Lambda

后端 未结 5 1987
醉话见心
醉话见心 2021-01-17 09:40

I\'m using API Gateway\'s Proxy integration to call a Lambda. The output format specification is this follow JSON format:

{
  \"statusCode\": httpStatusCode,         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-17 10:12

    As of November 2018 this is possible using the multiValueHeaders field in the response instead of headers (see announcement).

    As an example instead of:

    {
      "statusCode": 200,
      "body": "testing multiple set-cookie headers",
      "headers": {
        "X-Test-Header": "baking experiment",
        "Set-Cookie": "cookie1=chocolate-chip",
        "Set-Cookie": "cookie2=oatmeal",
        "Content-Type": "text/plain"
      }
    }
    

    You can respond with:

    {
      "statusCode": 200,
      "body": "testing multiple set-cookie headers",
      "multiValueHeaders": {
        "X-Test-Header": ["baking experiment"],
        "Set-Cookie": ["cookie1=chocolate-chip", "cookie2=oatmeal"],
        "Content-Type": ["text/plain"]
      }
    }
    

    Note that you can use a mix of headers and multiValueHeaders:

    {
      "statusCode": 200,
      "body": "testing multiple set-cookie headers",
      "headers": {
        "X-Test-Header": "baking experiment",
        "Content-Type": "text/plain"
      },
      "multiValueHeaders": {
        "Set-Cookie": ["cookie1=chocolate-chip", "cookie2=oatmeal"]
      }
    }
    

    However using the same header in both will mean that the value under headers is dropped.

    See the documentation for more details.

    When using only the header field (as available prior to Nov 2018) I tried sending the following manually curated JSON as a response:

    {
      "statusCode": 200,
      "body": "testing multiple set-cookie headers",
      "headers": {
        "X-Test-Header": "baking experiment",
        "Set-Cookie": "cookie1=chocolate-chip",
        "Set-Cookie": "cookie2=oatmeal",
        "Content-Type": "text/plain"
      }
    }
    

    The cookies that API gateway returns in response to a CURL request are:

    < Content-Type: text/plain
    < Content-Length: 35
    < Connection: keep-alive
    < Date: Thu, 29 Sep 2016 11:22:09 GMT
    < Set-Cookie: cookie2=oatmeal
    < X-Test-Header: baking experiment
    < X-Cache: Miss from cloudfront
    

    As you can see the first Set-Cookie is dropped on the floor.

提交回复
热议问题