Default headers
The DefaultHeaders plugin adds the standard Server and Date headers into each response. Moreover, you can provide additional default headers and override the Server header.
Add dependencies
To use DefaultHeaders, add the ktor-server-default-headers artifact to your build script:
Install DefaultHeaders
To install the DefaultHeaders plugin to your application, pass it to the install function in the specified module. The following examples show how to install DefaultHeaders:
In an
embeddedServer()function call.In an explicitly defined
module()extension function on theApplicationclass.
The DefaultHeaders plugin can also be installed to specific routes. This might be useful if you need different DefaultHeaders configurations for different application resources.
Configure DefaultHeaders
Add additional headers
To customize a list of default headers, pass a desired header to install by using the header(name, value) function. The name parameter accepts an HttpHeaders value, for example:
To add a custom header, pass its name as a string value:
Override headers
To override the Server header, use a corresponding HttpHeaders value:
Note that the Date header is cached due to performance reasons and cannot be overridden by using DefaultHeaders.