Search code examples
amazon-web-servicesaws-lambdaaws-api-gateway

Use multiple same header field in Lambda Proxy Integration


I'm writing a Lambda function that returns a response for Lambda Proxy Integration in Python. The API expects headers to be a dictionary.

{
    "isBase64Encoded": true|false,
    "statusCode": httpStatusCode,
    "headers": { "headerName": "headerValue", ... },
    "body": "..."
}

This requires each header field to be unique, so there is no way to use multiple Set-Cookie's. I've already tried to convert the dictionary to a list of tuples

{
    "isBase64Encoded": true|false,
    "statusCode": httpStatusCode,
    "headers": [ ("headerName": "headerValue"), ... ],
    "body": "..."
}

but API gateway complains Malformed Lambda proxy response.

Any idea how to set headers with the same name?


Solution

  • It is currently not possible to send multiple cookies with lambda integration.

    If you send multiple set-cookie, then it will take the last one. ok, such a junk implementation right.

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

    Let us see other avaialable options,

    Lambda@Edge:

    Here is what I find working with Lambda@Edge,

    You can create a lambda function for viewer response and modify the header to set cookies.

    'use strict';
    
    exports.handler = (event, context, callback) => {
       const response = event.Records[0].cf.response;
       const headers = response.headers;
    
       // send via a single header and split it into multiple set cookie here.
       headers['set-Cookie'] = 'cookie1';
       headers['Set-Cookie'] = 'cookie2';
    
       callback(null, response);
    };
    

    API Gateway Integration Request mapping:

    Here is what I found and got working with integration request,

    enter image description here

    enter image description here

    Hope it helps.