从 AWS lambda 设置 http 响应标头 [英] Setting http response header from AWS lambda
问题描述
我的 API 网关/Lambda 设置返回一个 HTTP 响应标头:Lambda 使用回调函数将值作为 JSON 的一部分返回并且 Integration Response 将其映射到 HTTP 标头(使用 integration.response.body
)
My API Gateway/Lambda setup returns an HTTP response header:
Lambda uses callback function to return the value as part of a JSON
and the Integration Response maps it into a HTTP header (using integration.response.body
)
使用此解决方案,值在正文和标题中都被发送回.
With this solution, the values are sent back both in the body and the header.
如何在不复制响应正文中的值的情况下映射来自 Lambda 响应的标头?
How can I map headers from the Lambda response without duplicating the values in the response body?
推荐答案
如果您启用了 Lambda 代理集成,您可以将响应标头设置为 Lambda 输出的一部分,API Gateway 会将它们作为对客户端的 HTTP 响应的一部分.
If you have Lambda proxy integration enabled, you can set the response headers as part of Lambda output and API Gateway will return them as part of the HTTP response to the client.
Node.js 示例:
Node.js example:
callback(null, {
"isBase64Encoded": false, // Set to `true` for binary support.
"statusCode": 200,
"headers": {
"header1Name": "header1Value",
"header2Name": "header2Value",
},
"body": "...",
});
其中 headers
可以为 null 或未指定(如果不返回额外的响应头).
where headers
can be null or unspecified if no extra response headers are to be returned.
这篇关于从 AWS lambda 设置 http 响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!