Grabbing response body


I’m trying to grab the response body. I’m attempting to do this as follows (see offending code in red):

Here, check out the documentation on exec and session:

exec { session =>
  // displays the content of the session in the console (debugging only)

  // return the original session

exec { session =>
  // return a new session instance with a new "foo" attribute whose value is "bar"
  session.set("foo", "bar")

exec can take an Expression[Session], which basically means it can consume a function that takes a Session object and returns a Session object: exec(Session => Session)

The docs explain further if you have an interest (it really returns a Validation[Session], but implicits do the work for ya).

In your code, you’re not returning a Session–you’re returning nothing, which is what the compiler is complaining about when it says it’s finding Unit (akin to void) instead of Validation[Session] (although, again, you only need to return a Session thanks to implicits).

What you’re looking for is something like


.exec((session: Session) => {
val aspResponse = session(“responseBody”).as[String]
return session // or you can just write ‘session’ (no quotes), as the last line is always returned


But wait! The above code isn’t really doing anything behavior-wise. aspResponse is a local variable in the scope of the anonymous function, so it ceases to exist after returning. Most likely, you want to store the response body to retrieve later, in which case you want to store it in the session:


.exec((session: Session) => { val aspResponse = session("responseBody").as[String] session.set("aspResponse", aspResponse) })


Take a look at the session API for more information. Note here that since sessions are immutable, session.set returns a new session. Hence, you’re returning a new session with the response body saved under the key “aspResponse.” This also implies that the session you return replaces the current session.

Hope this helps!

EDIT: ok, so it’s late and completely overlooked the fact that you already have the response body saved in the session (haha). The takeaway is still the same–you gotta return Session.

This helps tremendously. Thanks so much!