I'm attempting to use spray-client and spray-httpx and I'm having trouble figuring out how to convert 'set-cookie' headers from HttpResponse
to a 'cookie' header that I'd like to set on an HttpRequest
val responseSetCookieHeaders = response.headers filter { _.name == "Set-Cookie" }
...
val requestCookieHeader:HttpHeader = ???
...
addHeader(requestCookieHeader) ~> sendReceive ~> { response => ??? }
I do see spray.http.HttpHeaders.Cookie
, but I see no way to convert from an instance of HttpHeader
to HttpCookie
...
HttpHeaders.Cookie
is a case class with an unapply
method. So you can extract it from response with a simple function:
def getCookie(name: String): HttpHeader => Option[HttpCookie] = {
case Cookie(cookies) => cookies.find(_.name == name)
}
That's a bit more general case, but i think the solution is clear.
I would do this in the following way:
// some example response with cookie
val httpResponse = HttpResponse(headers = List(`Set-Cookie`(HttpCookie("a", "b"))))
// extracting HttpCookie
val httpCookie: List[HttpCookie] = httpResponse.headers.collect { case `Set-Cookie`(hc) => hc }
// adding to client pipeline
val pipeline = addHeader(Cookie(httpCookie)) ~> sendReceive
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With