Help us understand the problem. What is going on with this article?

Lambdaで複数値を持ったヘッダーを返す方法

More than 1 year has passed since last update.

はじめに

Lambda+S3でWebアプリを作った際にLambdaからSet-Cookieで複数のCookieを同時に設定したかったのですが、ちょっとハマったのでメモがてらここに残しておきます。

環境

  • Node.js 10.X
  • Lambdaプロキシ統合利用
  • メソッドはGET

コード

multiValueHeadersで設定できます。valueを配列にすれば良いだけみたいですね。ちなみに僕はJSONをJSON.stringifyして1時間くらいハマりました。気をつけましょう。

exports.handler = async (event, context, callback) => {
    const response = new Response();
    response.statusCode = 200;
    response.multiValueHeaders = { "Set-Cookie": [`KEY1=VALUE1`, `KEY2=VALUE2`] };
    response.body = JSON.stringify({ result: "succeed" });
    callback(null, response);
};

class Response {
    constructor() {
        this.statusCode = "";
        this.headers = {};
        this.multiValueHeaders = {};
        this.body = {};
    }
}
shinbunbun_
そこら辺に転がってるただの高三
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away