如何将标头添加到蒸气响应(CACHE-CORTOL)中



我使用返回 Future<Content>的get处理程序的控制器。我想在响应中添加一个标头(Cache-Contol是特定的)。我当时认为这应该很容易,但我没有找到该怎么做。在这种情况下,哪种添加标头的方法?当我们使用Content而不是Response

解决问题,您可以像这样写终点

struct Something: Content {
    let text: String
}
router.get("customresponse") { req -> Future<Response> in
    return try Something(text: "Hello world").encode(for: req).map { response in
        response.http.headers.add(name: .cacheControl, value: "something")
        return response
    }
}

Mike的答案是对我有用的。作为进一步的参考,我使用一个值将缓存延长了1天。

req.headers.add(name: .cacheControl, value: "public, max-age=86400")

最新更新