respondWithDefaultHeaders

Signature

def respondWithDefaultHeaders(responseHeaders: HttpHeader*): Directive0
def respondWithDefaultHeaders(responseHeaders: immutable.Seq[HttpHeader]): Directive0

Description

Adds the given HTTP headers to all responses coming back from its inner route only if a respective header with the same name doesn’t exist yet in the response.

This directive transforms HttpResponseHttpResponse and ChunkedResponseStart messages coming back from its inner route by potentially adding the given HttpHeaderHttpHeader instances to the headers list. A header is only added if there is no header instance with the same name (case insensitively) already present in the response.

See also respondWithDefaultHeader if you’d like to add only a single header.

Example

The respondWithDefaultHeaders directive is equivalent to the respondWithDefaultHeader directive which is shown in the example below, however it allows including multiple default headers at once in the directive, like so:

Scala
respondWithDefaultHeaders(
  Origin(HttpOrigin("https://akka.io")),
  RawHeader("X-Fish-Name", "Blippy")) { ??? }
Java
final List<HttpHeader> headers = Arrays.asList(
        Origin.create(HttpOrigin.parse("https://akka.io")),
        RawHeader.create("X-Fish-Name", "Blippy"));
respondWithDefaultHeaders(headers, () ->
        /*...*/
        complete("Blip!"));

The semantics remain the same however, as explained by the following example:

Scala
// custom headers
val blippy = RawHeader("X-Fish-Name", "Blippy")
val elTonno = RawHeader("X-Fish-Name", "El Tonno")

// format: OFF
// by default always include the Blippy header,
// unless a more specific X-Fish-Name is given by the inner route
val route =
  respondWithDefaultHeader(blippy) {  //  blippy
    respondWithHeader(elTonno) {      // /  el tonno
      path("el-tonno") {              // | /
        complete("¡Ay blippy!")       // | |- el tonno
      } ~                             // | |
      path("los-tonnos") {            // | |
        complete("¡Ay ay blippy!")    // | |- el tonno
      }                               // | |
    } ~                               // | x
    complete("Blip!")                 // |- blippy
  } // x
// format: ON

// tests:
Get("/") ~> route ~> check {
  header("X-Fish-Name") shouldEqual Some(RawHeader("X-Fish-Name", "Blippy"))
  responseAs[String] shouldEqual "Blip!"
}

Get("/el-tonno") ~> route ~> check {
  header("X-Fish-Name") shouldEqual Some(RawHeader("X-Fish-Name", "El Tonno"))
  responseAs[String] shouldEqual "¡Ay blippy!"
}

Get("/los-tonnos") ~> route ~> check {
  header("X-Fish-Name") shouldEqual Some(RawHeader("X-Fish-Name", "El Tonno"))
  responseAs[String] shouldEqual "¡Ay ay blippy!"
}
Java
//custom headers
final RawHeader blippy = RawHeader.create("X-Fish-Name", "Blippy");
final HttpHeader akka = Origin.create(HttpOrigin.parse("https://akka.io"));
final List<HttpHeader> defaultHeaders = Arrays.asList(blippy, akka);
final RawHeader elTonno = RawHeader.create("X-Fish-Name", "El Tonno");

// format: OFF
// by default always include the Blippy and Akka headers,
// unless a more specific X-Fish-Name is given by the inner route
final Route route =
        respondWithDefaultHeaders(defaultHeaders, () ->            // blippy and akka
                respondWithHeader(elTonno, () ->                   // / el tonno
                        path("el-tonno", () ->                     // | /
                                complete("¡Ay blippy!")            // | |- el tonno
                        ).orElse(                                  // | |
                                path("los-tonnos", () ->           // | |
                                        complete("¡Ay ay blippy!") // | |- el tonno
                                )                                  // | |
                        )                                          // | |
                ).orElse(                                          // | x
                        complete("Blip!")                          // |- blippy and akka
                )                                                  // x
        );
//format: ON

testRoute(route).run(HttpRequest.GET("/"))
        .assertHeaderExists("X-Fish-Name", "Blippy")
        .assertHeaderExists("Origin", "https://akka.io")
        .assertEntity("Blip!");

testRoute(route).run(HttpRequest.GET("/el-tonno"))
        .assertHeaderExists("X-Fish-Name", "El Tonno")
        .assertEntity("¡Ay blippy!");

testRoute(route).run(HttpRequest.GET("/los-tonnos"))
        .assertHeaderExists("X-Fish-Name", "El Tonno")
        .assertEntity("¡Ay ay blippy!");

See the respondWithDefaultHeader directive for an example with only one header.

The source code for this page can be found here.