Setting Cookie in http response header from AWS lambda Node JS

元气小坏坏 提交于 2019-12-10 11:34:17

问题


I have a Lambda proxy integration enabled, and setting the response headers as part of Lambda output and API Gateway that will return them as part of the HTTP response to the client.

Sample code:

callback(null, {
    "statusCode": 302,
    "Location" : "https://somewebsite.com"
    "headers": { "headerName": "headerValue", ... },
    "body": "..."
});

I need to send out 3 cookies in the headers. I tried. But, failed:

callback(null, {
    "statusCode": 302,
    "Location" : "https://somewebsite.com"
    "headers": { "Set-Cookie": [cookie1String, cookie2String, cookie3String] },
    "body": "..."
});

[Edit] I concatenated the cookie and passed in as the response, the client gets the cookie. But when the client calls the target in "location", the request does not have the cookie in the header.

callback(null, {
    "statusCode": 302,
    "Location" : "https://somewebsite.com"
    "headers": { "Set-Cookie": c1=cookie1String;c2=cookie2String; c3=cookie3String] },
    "body": "..."
});

Please help in sending these 3 cookies out to my client.


回答1:


API gateway does not let you map the same header more than once. I got around by using different casing to set-cookie method.

callback(null, {
    "statusCode": 302,
    "Location" : "https://somewebsite.com"
    "headers": { "Set-Cookie": cookie1, "set-Cookie": cookie2 },
    "body": "..."
});



回答2:


Use multiValueHeaders instead of headers.

    const response = {
        isBase64Encoded: true,
        statusCode: 200,
        multiValueHeaders : {"Set-Cookie": [`language=${language}`, `theme=${theme}`]},
        body: JSON.stringify('User profile set successfully')
    };
    callback(null, response);

If you need it to be smarter, consider something like

function createHeaders(headers) {
  const defaultHeaders = {
    'Access-Control-Allow-Origin': '*',
  };
  const allHeaders = Object.assign({}, defaultHeaders, headers);
  const singleValueHeaders = {};
  const multiValueHeaders = {};
  Object.entries(allHeaders).forEach(([key, value]) => {
    const targetHeaders = Array.isArray(value) ? multiValueHeaders : singleValueHeaders;
    Object.assign(targetHeaders, { [key]: value });
  });

  return {
    headers: singleValueHeaders,
    multiValueHeaders,
  };
}

Then use it in the callback function.

callback(null, {
  statusCode: status || 200,
  body: JSON.stringify(body),
  ...createHeaders({ 'Set-Cookie': cookie }),
});



回答3:


I would say that your issue is related to the fact that your response object in the callback is not formatted the way the api gateway expects.

These links reference aws documentation specifically to that.

http://docs.aws.amazon.com/apigateway/latest/developerguide/handle-errors-in-lambda-integration.html

Issue with your code...

  • 'location' does not look like a valid property
  • Make sure your header key/value pairs are actual JSON objects using something like JSON.stringify

Don't forget to enable logs for both api gateway and lambda with full requests and responses. These two logs will help you debug.



来源:https://stackoverflow.com/questions/45111150/setting-cookie-in-http-response-header-from-aws-lambda-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!