3
votes

I want to response a 304 response with Last-Modified header.

At first I use Error response to implement.

Handler.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 in endpoints

"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')"

      }
    }
}

However, I find it on Lambda doc.

If an error is provided, callback parameter is ignored.

So, this doesn't work.

Is there any solution to response a 304 response with header?

Updated:

Is it possible to return a Error object and map responses 304 in s-function? Below code can't map to 304.

s-funtion.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"
      }
}

Handler.js

return cb({
  "status" : "304 Not Modified",
  "Last-Modified": UpdateDate
), null);

I also try this. It can mapping to 304 but header can't get "integration.response.body.errorMessage.Last-Modified"

return cb(JSON.stringify({
  "status" : "304 Not Modified",
  "Last-Modified": UpdateDate
}), null);

I try $util.parseJson but not working on responseParameter.

Invalid mapping expression specified:$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"
  },
1
Please look at Ryan's link in Option 2. Your update does not actually return an error. - Bob Kinney
@BobKinney It's return an error. In cloudWatch Logs: { "errorMessage": "{\"status\":\"304 Not Modified\",\"Last-Modified\":\"2016-07-22T01:58:10.857Z\"}" } - Jim
Side note, 304's should not return response bodies according to W3. Just referring to your 1st example. - arjabbar
Thanks, finally I only put 304 status code in response and null body to implement cache control. And setting Last-Modified header in 200 response . - Jim

1 Answers

3
votes

To return status 304 in your API, you would need to throw an error from your Lambda function. It is possible to return the "Last-Modified" value in the error message from your Lambda function and route that to the "Last-Modified" header in the API response.

For details have a look at Option 2 here

Thanks, Ryan