Getting session attribute in HTTP check after setting in body

I have the following use case, I want to expect a status code based on the request body of an HTTP request.

My code currently looks like this:

`

http("Check is authorized")
  .post(s"/v1/isAuthorized")
  .body(StringBody(session => {
    session.set("expectedStatusCode", if (maybeGrantedPermission.isDefined) 204 else 401)

    **"my body"**
  }))
  .check(status.is(session => session("expectedStatusCode").validate[Int]))

`

Unfortunately I always get this error message:

`

Request:
Check is authorized: KO Check validator resolution crashed: No attribute named ‘expectedStatusCode’ is defined

Ok I seems I understand why it does not work, the Session is immutable.
But how would I do such a thing then?