respondWithHeaders

Signature

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

Description

Adds the given HTTP headers to all responses coming back from its inner route.

This directive transforms HttpResponseHttpResponse and ChunkedResponseStart messages coming back from its inner route by adding the given HttpHeaderHttpHeader instances to the headers list.

See also respondWithHeader if you’d like to add just a single header.

Example

Scala
val route =
  path("foo") {
    respondWithHeaders(RawHeader("Funky-Muppet", "gonzo"), Origin(HttpOrigin("https://akka.io"))) {
      complete("beep")
    }
  }

// tests:
Get("/foo") ~> route ~> check {
  header("Funky-Muppet") shouldEqual Some(RawHeader("Funky-Muppet", "gonzo"))
  header[Origin] shouldEqual Some(Origin(HttpOrigin("https://akka.io")))
  responseAs[String] shouldEqual "beep"
}
Java
final HttpHeader gonzo = RawHeader.create("Funky-Muppet", "gonzo");
final HttpHeader akka = Origin.create(HttpOrigin.parse("https://akka.io"));

final Route route = path("foo", () ->
        respondWithHeaders(Arrays.asList(gonzo, akka), () ->
                complete("beep")
        )
);

testRoute(route).run(HttpRequest.GET("/foo"))
        .assertHeaderExists("Funky-Muppet", "gonzo")
        .assertHeaderExists("Origin", "https://akka.io")
        .assertEntity("beep");
The source code for this page can be found here.