如何向Vapor响应添加标头(Cache-Control)

问题描述 投票:3回答:1

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

vapor
1个回答
4
投票

要解决这个问题,您可以像这样编写端点

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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.