3

我想用 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"
  },
4

1 回答 1

3

要在 API 中返回状态 304,您需要从 Lambda 函数中引发错误。可以从您的 Lambda 函数返回错误消息中的“Last-Modified”值,并将其路由到 API 响应中的“Last-Modified”标头。

有关详细信息,请查看此处的选项 2

谢谢,瑞安

于 2016-07-19T18:36:26.367 回答