HTTPプロキシインテグレーションの使用クッキーにアクセスしてjsonレスポンスに追加したいと思います。それは可能ですか?AWS Gateway APIを使用して、クッキーにアクセスできますか?
答えて
バックエンドでクライアントから送信されたクッキーにアクセスするには、メソッド要求ヘッダーから統合要求ヘッダーへのマッピングをセットアップする必要があります。
これらの手順では、APIゲートウェイで簡単な方法を設定していることを前提としています。メソッド要求の下では、バックエンド
- で
アクセスクッキーは、「クッキー」
- 統合要求の下の名前のHTTPリクエストヘッダを作成し、名前を「クッキー」とのHTTPヘッダを作成し、 "Mapped from"の値は
method.request.header.Cookie
です。 - また、この方法でCORSを設定する必要があります。参照:http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html
- APIを展開し、ブラウザ/クライアントでAPIゲートウェイエンドポイントにリクエストしてください。ブラウザーから送信されたCookieヘッダー値を使用してHTTPバックエンドに要求が送信されます。
は応答
にクッキーを追加することはできセットアップ方法構成の統合応答/メソッド応答側のために類似の様式でSet-Cookie
レスポンスヘッダ。メソッドレスポンスの下
- 、統合レスポンスの設定レスポンスヘッダ
Set-Cookie
とヘッダーのマッピングおよびマッピング値integration.response.header.Set-Cookie
の下の名前Set-Cookie
Set-Cookie
レスポンスヘッダーの設定をサポートしています。バックエンドが複数のSet-Cookieヘッダーを設定しようとすると、最後のものだけが設定されます。 https://forums.aws.amazon.com/thread.jspa?messageID=701434論理的には、それは助けなければならないと私は同じことを行う。しかし、ログのAPIゲートウェイがサーバーが "Set-Cookie"ヘッダーを返さないことを示していれば、私は100%確実であると確信しています。前に同じ問題に直面したことはありますか?何か案は? ありがとう) –
@SergeyPotapovドメインが一致しないため、localhost上でこれをテストしていると動作しません。ラムダのURL自体を開くとうまくいくはずです。そうでない場合は、メソッド/統合レスポンスを検証する必要があります –
API Gatewayメソッドで "Use Lambda Proxy integration"オプションをチェックすると、要求ヘッダーはevent
変数を介してラムダ関数に渡されます。 API Gatewayはまた、あなたのコールバック関数からの異なる応答を期待します。この応答形式は、Set-Cookie
ヘッダーを指示するために使用できます。例えば、
callback(null, {
statusCode: 200,
headers: {'Set-Cookie': 'key=val'},
body: 'Some response'
})`
このアプローチは、メソッドリクエストまたはメソッドレスポンスを必要としないという利点があります。
各リクエスト後にこのロジックを使用してCookie値をローテーションするラムダ関数のサンプルを示します。
exports.handler = (event, context, callback) => {
var cookies = getCookiesFromHeader(event.headers);
var old_cookie = cookies.flavor;
var new_cookie = pickCookieFlavor(old_cookie);
return callback(null, {
statusCode: 200,
headers: {
'Set-Cookie': setCookieString('flavor', new_cookie),
'Content-Type': 'text/plain'
},
body: 'Your cookie flavor was ' + old_cookie + '. Your new flavor is ' + new_cookie + '.'
});
};
/**
* Rotate the cookie flavor
*/
function pickCookieFlavor(cookie) {
switch (cookie) {
case 'peanut':
return 'chocolate';
case 'chocolate':
return 'raisin and oat';
default:
return 'peanut';
}
}
/**
* Receives an array of headers and extract the value from the cookie header
* @param {String} errors List of errors
* @return {Object}
*/
function getCookiesFromHeader(headers) {
if (headers === null || headers === undefined || headers.Cookie === undefined) {
return {};
}
// Split a cookie string in an array (Originally found http://stackoverflow.com/a/3409200/1427439)
var list = {},
rc = headers.Cookie;
rc && rc.split(';').forEach(function(cookie) {
var parts = cookie.split('=');
var key = parts.shift().trim()
var value = decodeURI(parts.join('='));
if (key != '') {
list[key] = value
}
});
return list;
};
/**
* Build a string appropriate for a `Set-Cookie` header.
* @param {string} key Key-name for the cookie.
* @param {string} value Value to assign to the cookie.
* @param {object} options Optional parameter that can be use to define additional option for the cookie.
* ```
* {
* secure: boolean // Watever to output the secure flag. Defaults to true.
* httpOnly: boolean // Watever to ouput the HttpOnly flag. Defaults to true.
* domain: string // Domain to which the limit the cookie. Default to not being outputted.
* path: string // Path to which to limit the cookie. Defaults to '/'
* expires: UTC string or Date // When this cookie should expire. Default to not being outputted.
* maxAge: integer // Max age of the cookie in seconds. For compatibility with IE, this will be converted to a
* `expires` flag. If both the expires and maxAge flags are set, maxAge will be ignores. Default to not being
* outputted.
* }
* ```
* @return string
*/
function setCookieString(key, value, options) {
var defaults = {
secure: true,
httpOnly: true,
domain: false,
path: '/',
expires: false,
maxAge: false
}
if (typeof options == 'object') {
options = Object.assign({}, defaults, options);
} else {
options = defaults;
}
var cookie = key + '=' + value;
if (options.domain) {
cookie = cookie + '; domain=' + options.domain;
}
if (options.path) {
cookie = cookie + '; path=' + options.path;
}
if (!options.expires && options.maxAge) {
options.expires = new Date(new Date().getTime() + parseInt(options.maxAge) * 1000); // JS operate in Milli-seconds
}
if (typeof options.expires == "object" && typeof options.expires.toUTCString) {
options.expires = options.expires.toUTCString();
}
if (options.expires) {
cookie = cookie + '; expires=' + options.expires.toString();
}
if (options.secure) {
cookie = cookie + '; Secure';
}
if (options.httpOnly) {
cookie = cookie + '; HttpOnly';
}
return cookie;
}
- 1. AWS API Gateway - Elastic Beanstalk - 制限付きアクセス
- 2. 応答を使用してaws api gatewayからリダイレクト302
- 3. Javascript用AWS API Gateway SDKの使用
- 4. aws api gatewayのaws proxyを使用してアイテムフォームのdynamodbを取得できませんか?
- 5. AWS API Gatewayカスタムドメイン
- 6. AWS API Gatewayのモックエンドポイントをデフォルトに設定していますか?
- 7. httpsリダイレクトなしでAWS API Gatewayを使用する方法
- 8. WSO2 APIマネージャでOracle API Gatewayを使用できますか?
- 9. AWS API GATEWAY - Swaggerテンプレートを使用したAPIのインポートとエクスポート
- 10. AWS API Gateway with AWS WAF
- 11. AWS API Gateway - > Lambdaのパフォーマンスを改善できますか?
- 12. メッセージ:aws gateway apiにアクセスしようとすると「内部サーバーエラー」
- 13. カスタムドメインAWS API-Gateway 403
- 14. AWS API Gateway return HTML
- 15. AWS API Gatewayのカスタムオーソラムダ
- 16. AWS api gateway for javascript?
- 17. AWS Api Gatewayのパラメータバリデーション
- 18. AWS API Gateway Methodレスポンスステータス
- 19. AWS API Gateway 403:Forbidden
- 20. AWS API Gateway Method Authorization
- 21. AWS API Gatewayのリソースとして、VPCのHTTPエンドポイントを指定できますか?
- 22. AWS Api Gateway用BOTO3を使用したCORSの自動化
- 23. AWS Api Gatewayパスパラメータでカスタムオーサライザポリシー
- 24. AWS API GatewayはOpenAPI 3.0をサポートしていますか?
- 25. Awsラムダ/ Api-Gateway/Cognitoが協力して
- 26. AWS API Gateway - CLIまたはGO aws-sdkを使用してルートリソースにメソッドを追加する方法
- 27. Cloudformation API Gatewayラムダプロキシインテグレーションを使用
- 28. アンドロイドのAWS api gatewayの使い方は?
- 29. AWS API Gatewayでステージを作成できません
はい、間違いありません。すべてのリクエストパラメータ(ヘッダー、クエリ文字列、パス、本文)にアクセスでき、統合要求内のマッピングテンプレートをHTTPバックエンドに変換できます。 –