# Modifying the response for next request

**URL:** <https://community.gatling.io/t/modifying-the-response-for-next-request/5724>\
**Category:** Gatling (Open-Source)\
**Created:** [July 20, 2020, 11:40am UTC](https://community.gatling.io/t/modifying-the-response-for-next-request/5724 "2020-07-20T11:40:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Snow\_Man](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@Snow\_Man](https://community.gatling.io/u/Snow_Man)\
**Post date:** [July 20, 2020, 11:40am UTC](https://community.gatling.io/t/modifying-the-response-for-next-request/5724/1 "2020-07-20T11:40:57Z")

</div>

My response body is  
["Admin","user","Su"]

At times it will return  
["Admin","user"] or ["Admin"]

My next request uses a queryparam  
As ?roles=Admin,user,Su

Or ?roles=Admin,user

Based on previous response.

Am trying to save response as bodyString  
And then retrieving it from session and removing the doublequotes and square brackets using string replace and storing the value in a var and then using it in next request.

When i print the var it does print the values correctly

Eg: Admin,user,Su

But its giving me empty value for my variable when i use the var in the next request.

Any help why i am getting an empty value.

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [July 20, 2020, 1:54pm UTC](https://community.gatling.io/t/modifying-the-response-for-next-request/5724/2 "2020-07-20T13:54:26Z")

</div>

Hi Snow Man,

Are you sure you return the modified session after storing into your value? (in scala, you should avoid using var keyword)  
[https://gatling.io/docs/current/session/session\_api#setting-attributes](https://gatling.io/docs/current/session/session_api#setting-attributes)

Hence the session is immutable, if you are not returning it once modified, the next request will not be aware of this modified version.

---

<div class="post-metadata">

**Author:** ![Snow\_Man](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@Snow\_Man](https://community.gatling.io/u/Snow_Man)\
**Post date:** [July 20, 2020, 3:18pm UTC](https://community.gatling.io/t/modifying-the-response-for-next-request/5724/3 "2020-07-20T15:18:03Z")

</div>

Hi Sebastian,

Thanks for your reply. Yes i am returning.

My test is as below. Pls ignore any typos or case in typing.

Def getRoles()={

Var newRoles=""

exec{session=\>

newRoles= session("roles").toString.replaceAll("","")  
session.set("newRoles",newRoles)  
session  
}

.exec(http("getreq")  
.get("/summary/${newRoles}")

}

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [July 20, 2020, 4:16pm UTC](https://community.gatling.io/t/modifying-the-response-for-next-request/5724/4 "2020-07-20T16:16:50Z")

</div>

Hi Snow Man,

Actually, you are returning the pristine session.

As said, the session is immutable.

def getRoles()={  
exec{ session=\>  
val newRoles = session(“roles”).toString.replaceAll("","") // \<= seem that something is missing in replaceAll arguments here copy pasta issue?  
session.set(“newRoles”, newRoles) // set returns the updated session  
}  
.exec(http(“getreq”)  
.get("/summary/${newRoles}")  
}

The above code should work better

---

<div class="post-metadata">

**Author:** ![Snow\_Man](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@Snow\_Man](https://community.gatling.io/u/Snow_Man)\
**Post date:** [July 20, 2020, 7:46pm UTC](https://community.gatling.io/t/modifying-the-response-for-next-request/5724/5 "2020-07-20T19:46:58Z")

</div>

Thank you for the prompt reply,

I have tried just exactly as you said, however the newRoles attribute in the session now holds all the session information with the modified roles values in "roles" key.

So ${newRoles} is giving me all the session information rather than just the string value we have set.

Am i doing anything wrong.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [July 21, 2020, 7:41am UTC](https://community.gatling.io/t/modifying-the-response-for-next-request/5724/6 "2020-07-21T07:41:38Z")

</div>

Please check what is the value of session(“roles”) at the beginning. How do you capture it in the first place?

You would also want to check (with println? but ensure that is not the last instruction when returning something like the session) that each step is doing its job.
