1

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.

Urist McDev
  • 498
  • 3
  • 14
Raindog
  • 1,468
  • 3
  • 14
  • 28

1 Answers1

3

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.

Ashalynd
  • 12,363
  • 2
  • 34
  • 37
  • This seems to be the problem with setting the cookie for sure. Can you answer the 2nd part as to how I would inspect the content of the POST request so I can compare it to a known valid POST? – Raindog Oct 25 '13 at 01:56
  • This definitely fixes the problem. – Raindog Oct 25 '13 at 08:12
  • Regarding inspecting the request object: the easiest would be just to print it out in your log. It has the following methods defined: host, creds, method, path, headers, body, defaultCharset. body is of type `Option[org.apache.http.HttpEntity]` and headers is `List[(String, String)]`. I would recommend downloading dispatch distro from github: https://github.com/dispatch/dispatch and inspecting the code, it's definitely enlightening :) – Ashalynd Oct 25 '13 at 11:00
  • I finally have some intellisense after remaking the eclipse project w/ sbt. The problem I had was doing println(mypost) yielded "Req()". Intellisense showed me a method toRequest() which I could then do println(mypost.toRequest) and it showed something useful then. – Raindog Oct 25 '13 at 17:52
  • Fake it until you make it :) it's very confusing at the beginning, just give it a couple of months and keep reading and trying to understand the code other people wrote, you'll get better :) – Ashalynd Oct 25 '13 at 17:54