2
votes

I'm trying to retrieve a cookie from Akka HttpResponse

val httpRequest = HttpRequest(method=HttpMethods.POST, uri = uri, entity=params) val responseFuture: Future[HttpResponse] = Http().singleRequest(HttpRequest(uri = uri))

responseFuture2.flatMap { response =>
  println(response.entity)
  response.headers.collect {
    case hc =>
      println(hc)
  }
}

However I cannot find the cookie value in either response entity or response headers. I believe that the cookie jar should have been supported already in akka. https://github.com/spray/spray/pull/311

Does anyone has idea how can I retrieve the cookie by utilizing akka? Thanks in ad!

Here's how I've done it in python.

cookie_jar = cookielib.CookieJar() non_redirecting_opener = urllib2.build_opener(NoRedirectionProcessor, urllib2.HTTPCookieProcessor(cookie_jar)) response = non_redirecting_opener.open(request) cookies = {cookie.name: cookie for cookie in cookie_jar}

1

1 Answers

2
votes

It'll do the trick:

import akka.http.scaladsl.model.headers._

val responseFuture: Future[HttpResponse] = Http(context.system).singleRequest(HttpRequest(uri = "http://localhost:8080"))

responseFuture.onComplete(response => {
  val cookies = response.get.headers.collect {
    case c: `Set-Cookie` => c.cookie
  }
  println(cookies)
})

But you should not operate on Future directly but send it through actor system.