Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vapor

I have a controller using a get handler that returns a Future<Content>. I would like to add a header to the response (Cache-Control to be specific). I was thinking that it should be easy but I'm not finding how to do it. Which would be the way to add a header in this case? When we are working with Content instead of Response

like image 409
Carlos Avatar asked Jan 02 '23 05:01

Carlos


1 Answers

To solve the problem you could write your endpoint like this

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
    }
}
like image 115
imike Avatar answered Jan 08 '23 12:01

imike