skip to Main Content

I have a Lambda function running NodeJS 16x.

In the response I’m adding a cookie in the response headers as follows :

function GetResponse() {   
    const content = GetSomeHTML();
      
    const cookieName = 'MyCookie';
    const cookieValue = 'cookieValue123';
    const maxAge = 2 * 60 * 60; // 2 hours in seconds

    // Construct the Set-Cookie header
    const setCookieHeader = cookieName+'='+cookieValue+ '; Max-Age=' + maxAge + '; Path=/; HttpOnly';

    const response = {
        status: '307',
        statusDescription: 'OK',
        headers: {
            'content-type': [{
                key: 'Content-Type',
                value: 'text/html'},
                {
                key:'Set-Cookie',
                value:setCookieHeader
                }]
        },
        body: content,
    };
    
    return response;
}

When running the function, I’m getting the error :

The Lambda function returned an invalid entry in the headers object: The name of the header must be in lowercase and must match the value of key except for case.

Does anyone know how to solve that please ?

Thanks.

2

Answers


  1. The headers object seems misconstructed. I would have kept it simple with:

    headers: {
       'Content-Type': 'text/html',
       'Set-Cookie': setCookieHeader
    }
    

    but if you want to follow your structure:

    headers: {
        'content-type': [{
            key: 'Content-Type',
            value: 'text/html'
        }],
        'set-cookie': [{
            key: 'Set-Cookie',
            value: setCookieHeader
        }]
    }
    
    Login or Signup to reply.
  2. Header names are case-insensitive, so content-type is the same as Content-Type

    You can change you response like this

    const response = {
        status: '307',
        statusDescription: 'OK',
        headers: {
            'content-type': 'text/html',
            'set-cookie': setCookieHeader
        },
        body: content
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search