Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding headers to an Akka HTTP HttpRequest

I have an existing Akka HTTP HttpRequest and I want to add two headers to it.

val req: HttpRequest = ???
val hs: Seq[HttpHeader] = Seq(RawHeader("a", "b"))
req.addHeaders(hs)

Expected:

  • a new HttpRequest object with the additional headers

Actual:

  • .addHeaders expects a java.lang.Iterable and does not compile.

What is the recommended way of doing this in Scala?

There is a workaround, but it's a bit cludgy:

req.withHeaders(req.headers ++ hs)

Running Scala 2.12.8 and Akka HTTP 10.1.7.

like image 341
akauppi Avatar asked Jun 04 '26 19:06

akauppi


1 Answers

Another workaround that is maybe a tiny sliver less cludgy. This is approximately how addHeaders is defined in the source. I unfortunately have no clue why addHeaders is not exposed in the scala api.

req.mapHeaders(_ ++ hs)
like image 110
DrPhil Avatar answered Jun 06 '26 18:06

DrPhil