如何将 header 添加到 Vapor 响应 (Cache-Control)

How to add a header to a Vapor response (Cache-Control)

我有一个控制器使用 returns 一个 Future<Content> 的 get 处理程序。我想在响应中添加一个 header(具体来说是 Cache-Control)。我在想这应该很容易,但我找不到如何去做。在这种情况下,添加 header 的方法是什么?当我们使用 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 的回答很准确...这对我有用。作为进一步的参考,我使用一个值在所有 public 个缓存上将缓存延长 1 天。

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