Send Headers Automatically

This feature adds a default set of headers to HTTP responses. The list of headers is customizable, and the Date header is cachedto avoid building complex strings on each response.

This feature is defined in the class io.ktor.features.DefaultHeaders and no additional artifacts are required.

Usage

  1. fun Application.main() {
  2. ...
  3. install(DefaultHeaders)
  4. ...
  5. }

This will add Date and Server headers to each HTTP response.

Configuration

  • header(name, value) will add another header to the list of default headers
  1. fun Application.main() {
  2. ...
  3. install(DefaultHeaders) {
  4. header("X-Developer", "John Doe") // will send this header with each response
  5. }
  6. ...
  7. }
  • default Server header can be overriden by specifying your custom header:
  1. fun Application.main() {
  2. ...
  3. install(DefaultHeaders) {
  4. header(HttpHeaders.Server, "Konstructor")
  5. }
  6. ...
  7. }
  • The default Date header cannot be overridden. If you need to override it, do not install the DefaultHeaders feature and instead intercept the call manually