I send following headers in a request to my akka-http api: "Content-type": "application/json"
, "Accept": "application/json"
, "AppId": "some_id"
.
How do I get "AppId" custom header in my akka-http route?
(get & parameters("id")) { (id) =>
complete {
val appId = ?? // I want to get custom header here.
}
}
Thanks.
I actually prefer creating custom directive for things like authentication tokens, app ids and other parameters that are sort of mandatory for serving client's request. In your case it might look like this
which is used like
To make my example more interesting I added support of conditional response based on provided AppId.
You need to use one of the
HeaderDirectives
(HeaderDirectives docs) to extract the header. For example, if it's a custom one you can use headerValueByName which yields the value of the header, and rejects the route if the header was not present (if the header is optional you can useoptionalHeaderValueByName
):Happy hakking!