How to get http request header information from the server side using RestAPI spray

I am new to Scala and spray. I wrote a simple REST API in accordance with the instructions given in this blog post. http://www.smartjava.org/content/first-steps-rest-spray-and-scala

And everything works as expected.

Now I want to change the program for printing HTTP headers such as encoding, language, remote address, etc. I would like to print all the header information (the goal is to record this data)

But I could not find the correct documentation or examples. Can anyone help me do this.

+4
source share
2 answers

:

optionalHeaderValueByName("Encoding") { encodingHeader =>
  println(encodingHeader)
  complete("hello")
}

. , :

def logHeaders(): Directive0 = extract(_.request.headers).map(println)

logHeaders() {
  complete("hello")
}
+6

.

def logHeaders(innerRoute: Route): (RequestContext => Unit) = extract(_.request.headers) { headers =>
  headers.foreach(h => logger.info("header: {} = {}", h.name, h.value))
  innerRoute
}

:

logHeaders() {
  complete("hello")
}
+2

Source: https://habr.com/ru/post/1607252/


All Articles