[英]How can I generate a Set-Cookie integration response header in AWS API Gateway?
我目前正在使用亚马逊的API网关来创建一个直接与DynamoDB交互的REST API(使用“AWS服务”集成类型 - 中间没有lambda)。 一切正常,除了我想在第一个响应上返回一个Set-Cookie标头,用于后续调用API。
为简单起见(这里不担心安全性),我想使用context.requestId作为cookie的值。 问题是Set-Cookie标头需要的不仅仅是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文件。
有关响应 > 标题映射 > 映射值的 AWS文档,请参见 https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-method-settings-execution-console.html
对于映射值,请使用以下格式之一:
integration.response.header。 header-name其中header-name是后端的单值响应头的名称。 例如,要将后端响应的
Date
标头作为API方法的响应的Timestamp
标头返回, Response标头列将包含Timestamp条目,并且关联的Mapping值应设置为integration.response.header.Date 。 ...
因此,上面归结为DynamoDB支持的内容 。 通过在文档https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html#API_GetItem_ResponseElements中查看其中一个API示例,例如GetItem
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
头值
integration.response.header.x-amzn-RequestId
其他响应可能不包含此标头,但在这种情况下,可以启用请求跟踪,这将最终向后吐出X-Amzn-Trace-Id
标头
AWS响应参数映射文档提及映射的可用语法:
+--------------------------------------+------------------------+
| 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网关使用Velocity Template Language(VTL)引擎来处理集成请求和集成响应的正文映射模板。 映射模板将方法请求有效负载转换为相应的集成请求有效负载,并将集成响应主体转换为方法响应主体。
AWS上有一个指南 - 使用映射模板覆盖API的请求和响应参数和状态代码
模板看起来类似于下面。 我没有测试过它:
#set($cookieName = "id")
#set($cookieNameValSeparator = "=")
$input.json("$")
#set($context.responseOverride.header.Set-Cookie = "$cookieName$cookieNameValSeparator$context.requestId")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.