respondWithSingletonHeader
Adds a given HTTP header to all responses coming back from its inner route only if a header with the same name doesn’t exist yet in the response.
Signature
def respondWithSingletonHeader(responseHeader: HttpHeader): Directive0
Description
This directive transforms HttpResponse
and ChunkedResponseStart
messages coming back from its inner route by
potentially adding the given HttpHeader
instance to the headers list.
The header is only added if there is no header instance with the same name (case insensitively) already present in the
response. If you’d like to add more than one header you can use the respondWithSingletonHeaders directive instead.
Example
val respondWithMuppetHeader =
respondWithSingletonHeader(RawHeader("Funky-Muppet", "gonzo"))
val route =
path("foo") {
respondWithMuppetHeader {
complete("beep")
}
} ~
path("bar") {
respondWithMuppetHeader {
respondWithHeader(RawHeader("Funky-Muppet", "kermit")) {
complete("beep")
}
}
}
Get("/foo") ~> route ~> check {
headers.filter(_.is("funky-muppet")) === List(RawHeader("Funky-Muppet", "gonzo"))
responseAs[String] === "beep"
}
Get("/bar") ~> route ~> check {
headers.filter(_.is("funky-muppet")) === List(RawHeader("Funky-Muppet", "kermit"))
responseAs[String] === "beep"
}