amazon-web-services - 如何在 AWS API Gateway 中生成 Set-Cookie 集成响应 header ?

标签 amazon-web-services cookies http-headers aws-api-gateway api-gateway

我目前正在使用 Amazon 的 API Gateway 创建一个直接与 DynamoDB 交互的 REST API(使用“AWS 服务”集成类型——中间没有 lambda)。一切正常,除了我想在第一个响应时返回一个 Set-Cookie header ,以用于对 API 的后续调用。

为简单起见(这里不考虑安全性),我想使用 context.requestId 作为 cookie 的值。问题是 Set-Cookie header 需要的不仅仅是 cookie 的值;至少它还需要 cookie 的名称,格式为 CookieName=CookieValue ,实际上我还想为其设置其他参数,例如到期日期。

但是,似乎没有办法将上下文变量与“标题映射值”中的一些静态文本结合起来,因为我需要上述格式:https://docs.aws.amazon.com/apigateway/latest/developerguide/request-response-data-mappings.html#mapping-response-parameters

所以我的问题是:有什么我可以放入“标题映射值”框中来获得这种行为的吗? 类似于 'id='+context.requestId 的东西,但有效吗?我也愿意使用其他设置方法,例如 AWS CLI 或导入 OpenAPI 文件。

作为引用,这是有问题的 API Gateway 输入框:
enter image description here

最佳答案

简单的 header 映射

关于响应 > header 映射 > 映射值的 AWS 文档,页面 https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-method-settings-execution-console.html

For Mapping value, use one of the following formats:

integration.response.header.header-name where header-name is the name of a single-valued response header from the backend. For example, to return the backend response's Date header as an API method's response's Timestamp header, the Response header column will contain a Timestamp entry, and the associated Mapping value should be set to integration.response.header.Date. ...



以上归结为 DynamoDB 支持的内容。通过查看文档 https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html#API_GetItem_ResponseElements 中的 GetItem 等 API 示例之一
HTTP/1.1 200 OK
x-amzn-RequestId: <RequestId>
x-amz-crc32: <Checksum>
Content-Type: application/x-amz-json-1.0
Content-Length: <PayloadSizeBytes>
Date: <Date>
{ response json excluded for brevity}

所以我可能会尝试使用 x-amzn-RequestId映射中的 header 值
integration.response.header.x-amzn-RequestId

其他响应可能不包含此 header ,但在这种情况下,可以启用请求跟踪,最终会返回 X-Amzn-Trace-Id header

还能映射什么

AWS response param mapping docs提及可用的映射语法:
+--------------------------------------+------------------------+
| Mapped Data Source                   | Mapping expression     |
+--------------------------------------+------------------------+
| Integration response header          | integration.response.header.PARAM_NAME |
| Integration response header          | integration.response.multivalueheader.PARAM_NAME |
| Integration response body            | integration.response.body |
| Integration response body (JsonPath) | integration.response.body.JSONPath_EXPRESSION |
| Stage variable                       | stageVariables.VARIABLE_NAME |
| Context variable                     | context.VARIABLE_NAME that must be one of the supported context variables. |
| Static value                         | 'STATIC_VALUE'. The STATIC_VALUE is a string literal and must be enclosed within a pair of single quotes. |
+--------------------------------------+------------------------+

我们也知道 PARAM_NAME需要匹配正则表达式^[a-zA-Z0-9._$-]+$从同一个文档页面。

即使 'id='+context.requestId 也没有显示串联的示例。支持语法,没有什么可以阻止它在将来被删除。

另一种选择 - 映射模板

API Gateway uses Velocity Template Language (VTL) engine to process body mapping templates for the integration request and integration response. The mapping templates translate method request payloads to the corresponding integration request payloads and translate integration response bodies to the method response bodies.



AWS 上有一个指南 - Use a Mapping Template to Override an API's Request and Response Parameters and Status Codes

模板可能如下所示。我还没有测试过:
#set($cookieName = "id")
#set($cookieNameValSeparator = "=")
$input.json("$")
#set($context.responseOverride.header.Set-Cookie = "$cookieName$cookieNameValSeparator$context.requestId")

关于amazon-web-services - 如何在 AWS API Gateway 中生成 Set-Cookie 集成响应 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55150459/

相关文章:

javascript - 使用 jQuery 为输入字段、复选框等存储客户端 Cookie?

javascript - 使用 `GET` 的客户端请求表单,即使定义了 `POST`。 javascript iframe 是原因吗?

postgresql - terraform 和 aurora postgresql 的存储类型错误

swift - AWS Cognito credentialsProvider.login 始终显示 NIL (SWIFT)

php - 将 cookie 从浏览器传递到 Guzzle 6 客户端

javascript - Angular 5 - 如何重定向到具有特定 header 的外部 URL?

java - 了解 "Request Header is too large"

java - 将AWS S3配置为图像服务器

amazon-web-services - AWS Glue编写带分区的拼花地板

php - 打印时 IE 不重新发送 cookie