mapResponseHeaders • Akka HTTP (original) (raw)
Signature
def mapResponseHeaders(f: immutable.Seq[HttpHeader] => immutable.Seq[HttpHeader]): Directive0
Description
Changes the list of response headers that was generated by the inner route.
The mapResponseHeaders
directive is used as a building block for Custom Directives to transform the list of response headers that was generated by the inner route.
See Response Transforming Directives for similar directives.
Example
Scala
source`// adds all request headers to the response val echoRequestHeaders = extract(_.request.headers).flatMap(respondWithHeaders)
val removeIdHeader = mapResponseHeaders(.filterNot(.lowercaseName == "id")) val route = removeIdHeader { echoRequestHeaders { complete("test") } }
// tests: Get("/") ~> RawHeader("id", "12345") ~> RawHeader("id2", "67890") ~> route ~> check { header("id") shouldEqual None header("id2").get.value shouldEqual "67890" }`
Java
source`import static akka.http.javadsl.server.Directives.mapResponseHeaders;
import static akka.http.javadsl.server.Directives.complete; import static akka.http.javadsl.server.Directives.respondWithHeaders; import static akka.http.javadsl.server.Directives.mapResponseHeaders;
// adds all request headers to the response final Route echoRequestHeaders = extract( ctx -> ctx.getRequest().getHeaders(), headers -> respondWithHeaders(headers, () -> complete("test")) );
final Route route = mapResponseHeaders(headers -> { headers.removeIf(header -> header.lowercaseName().equals("id")); return headers; }, () -> echoRequestHeaders);
// tests: testRoute(route).run(HttpRequest.GET("/").withHeaders( Arrays.asList(RawHeader.create("id", "12345"),RawHeader.create("id2", "67890")))) .assertHeaderKindNotExists("id") .assertHeaderExists("id2", "67890");`
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.