respondWithHeaders

Signature

def respondWithHeaders(responseHeaders: HttpHeader*): Directive0 
def respondWithHeaders(firstHeader: HttpHeader, otherHeaders: 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
sourceval route =
  path("foo") {
    respondWithHeaders(RawHeader("Funky-Muppet", "gonzo"), Origin(HttpOrigin("http://pekko.apache.org"))) {
      complete("beep")
    }
  }

// tests:
Get("/foo") ~> route ~> check {
  header("Funky-Muppet") shouldEqual Some(RawHeader("Funky-Muppet", "gonzo"))
  header[Origin] shouldEqual Some(Origin(HttpOrigin("http://pekko.apache.org")))
  responseAs[String] shouldEqual "beep"
}
Java
sourceimport static org.apache.pekko.http.javadsl.server.Directives.complete;
import static org.apache.pekko.http.javadsl.server.Directives.path;
import static org.apache.pekko.http.javadsl.server.Directives.respondWithDefaultHeaders;
import static org.apache.pekko.http.javadsl.server.Directives.respondWithHeaders;

final HttpHeader gonzo = RawHeader.create("Funky-Muppet", "gonzo");
final HttpHeader pekko = Origin.create(HttpOrigin.parse("http://pekko.apache.org"));

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

testRoute(route)
    .run(HttpRequest.GET("/foo"))
    .assertHeaderExists("Funky-Muppet", "gonzo")
    .assertHeaderExists("Origin", "http://pekko.apache.org")
    .assertEntity("beep");