Vapor Custom headers in the Response using Middleware

We can create a new Middleware to add custom headers to the response:

// CustomHeaderMiddleware.swift 

import Vapor

class CustomHeaderMiddleware: Middleware {
    func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response> {
        return next.respond(to: request).map { response in
            response.headers.add(name: "Secure", value: "true")
            return response


Then we should use this middleware from Configure:

// Configure.swift

let customHeaderMiddleware = CustomHeaderMiddleware()

These changes will add the custom header to all the responses from the application.

blog comments powered by Disqus
<< The difference in sql queri...