我想用 Last-Modified 标头响应 304 响应。
起初我使用错误响应来实现。
处理程序.js
module.exports.handler = function(event, context, cb) {
const UpdateDate = new Date();
return cb("304 Not Modified", {
"Last-Modified": UpdateDate,
"body":{
"message": {}
}
});
};
端点中的 s-function.json
"responses": {
"304 Not Modified.*": {
"statusCode": "304",
"responseParameters": {
"method.response.header.Last-Modified": "integration.response.body.Last-Modified"
},
"responseModels": {
"application/json;charset=UTF-8": "Empty"
},
"responseTemplates": {
"application/json;charset=UTF-8": "$input.json('$.body')"
}
},
"default": {
"statusCode": "200",
"responseParameters": {
"method.response.header.Cache-Control": "'public, max-age=86400'",
"method.response.header.Last-Modified": "integration.response.body.Last-Modified"
},
"responseModels": {
"application/json;charset=UTF-8": "Empty"
},
"responseTemplates": {
"application/json;charset=UTF-8": "$input.json('$.body')"
}
}
}
但是,我在 Lambda 文档中找到了它。
如果提供错误,则忽略回调参数。
所以,这行不通。
是否有任何解决方案来响应带有标头的 304 响应?
更新:
是否可以在 s-function 中返回一个错误对象并映射响应 304?下面的代码无法映射到 304。
s-function.json
"responses": {
".*304 Not Modified.*": {
"statusCode": "304",
"responseParameters": {
"method.response.header.Cache-Control": "'public, max-age=86400'",
"method.response.header.Last-Modified": "integration.response.body.errorMessage.Last-Modified"
}
}
处理程序.js
return cb({
"status" : "304 Not Modified",
"Last-Modified": UpdateDate
), null);
我也试试这个。它可以映射到 304 但标头无法获取“integration.response.body.errorMessage.Last-Modified”
return cb(JSON.stringify({
"status" : "304 Not Modified",
"Last-Modified": UpdateDate
}), null);
我尝试了 $util.parseJson,但没有处理 responseParameter。
指定的映射表达式无效:$util.parseJson($input.path('$.errorMessage')).Last-Modified
"responseParameters": {
"method.response.header.Cache-Control": "'public, max-age=86400'",
"method.response.header.Last-Modified": "$util.parseJson($input.path('$.errorMessage')).Last-Modified"
},