Вопрос

I can't seem to set a cookie using Dispatch. The server resends a new session ID implying that the one I tried to send didn't get sent in the right way. Here is the code:

val domain = "myhost.com"
val host_req = host(domain).secure
val request = host_req / "path" / "path"

def post = request << Map("key" -> "SomeValue")

val response: Either[Throwable, Map[String, String]] =
    Http(post OK asHeaders).either()

//The cookie comes down in the form "Set-Cookie: SESSIONID=<somesession>; Path=/path/; HttpOnly"

//successfully retrieves the session id...
val sessionId = getSessionId(response)
println(sessionId)
val sessionCookie = new com.ning.http.client.Cookie(domain, "SESSIONID", sessionId, "/path/", -1, true)

request.POST.addCookie(sessionCookie)
def establishPost = request << Map("key" -> "SomeValue")
establishPost.addCookie(sessionCookie)

val establishResponse: Either[Throwable, Map[String, String]] =
    Http(establishPost OK asHeaders).either()


//Server sends down new SESSIONID...
//sessionId != getSEssionId(establishPost)

This is using the newest version of Dispatch. I'm trying to learn Scala as I go, and the only thing I can't figure out how to do either is inspect the establishPost object for its headers before it is sent as a request.

Это было полезно?

Решение

This should be better:

def reqWithParams = request << Map("key" -> "SomeValue")
val reqWithCookies = reqWithParams.addCookie(sessionCookie)

addCookie method returns the new object (the one with a cookie), but your code didn't use it.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top