【问题标题】:How to add a header to a Vapor response (Cache-Control)如何向 Vapor 响应添加标头(缓存控制)
【发布时间】:2019-03-01 20:24:11
【问题描述】:

我有一个控制器,它使用返回 Future<Content> 的 get 处理程序。我想在响应中添加一个标头(具体来说是 Cache-Control)。我在想这应该很容易,但我不知道该怎么做。在这种情况下,添加标题的方法是什么?当我们使用Content 而不是Response

【问题讨论】:

    标签: vapor


    【解决方案1】:

    为了解决这个问题,你可以这样写你的端点

    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
        }
    }
    

    【讨论】:

      【解决方案2】:

      Mike 的回答很准确……这对我有用。作为进一步的参考,我使用一个值将所有公共缓存上的缓存延长 1 天。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-07
        • 2011-10-08
        • 2020-07-23
        • 1970-01-01
        • 1970-01-01
        • 2021-10-20
        相关资源
        最近更新 更多