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 HttpResponse
HttpResponse
and ChunkedResponseStart
messages coming back from its inner route by adding the given HttpHeader
HttpHeader
instances to the headers list.
See also respondWithHeader if you’d like to add just a single header.
Example
- Scala
-
source
val route = path("foo") { respondWithHeaders(RawHeader("Funky-Muppet", "gonzo"), Origin(HttpOrigin("http://akka.io"))) { complete("beep") } } // tests: Get("/foo") ~> route ~> check { header("Funky-Muppet") shouldEqual Some(RawHeader("Funky-Muppet", "gonzo")) header[Origin] shouldEqual Some(Origin(HttpOrigin("http://akka.io"))) responseAs[String] shouldEqual "beep" }
- Java
-
source
import static akka.http.javadsl.server.Directives.complete; import static akka.http.javadsl.server.Directives.path; import static akka.http.javadsl.server.Directives.respondWithDefaultHeaders; import static akka.http.javadsl.server.Directives.respondWithHeaders; final HttpHeader gonzo = RawHeader.create("Funky-Muppet", "gonzo"); final HttpHeader akka = Origin.create(HttpOrigin.parse("http://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", "http://akka.io") .assertEntity("beep");