Call a function if check is success?

Hi,

I wanted to write a session attribute to a file only if the response returned the string ‘Success’.

I’m trying to do this:

.check(substring(“success”).onSuccess(writeThisToFile())

I see that this is not valid as the IDE says. Is there any way to call a function depending on the outcome of response check?

I have never personally tried this I can see there is a checkIf command, https://gatling.io/docs/2.3/http/http_check/, “Nested thenCheck will only be performed if condition is successful”.
There is a post on it here https://groups.google.com/forum/#!searchin/gatling/checkIf|sort:date/gatling/PySaRaG72NQ/Xat9LyoOAgAJ
Another approach could perhaps be to always save the variable from your check (saveAs), and then use an if / doIf as appropriate (see https://gatling.io/docs/2.3/general/scenario/) to perform your action/call your function. I’ve also never tried this so may or may not work :slight_smile:

Thanks David,

In my case, neither of them worked because:

  1. CheckIf - CheckIf can be nested only with a ‘thencheck’, you cannot call a function in ‘then’ clause. CheckIf expects only a check in its block, nothing else :frowning:
  2. Storing the attribute using saveAs then write the condition - The variable that I want to pass on to the function lies in the request that i’m making, not in the response. So I cannot ‘check a request’, neither can I use saveAs…

To sum it up - I want to write a session variable that I’m using in a request to a file depending on the response I receive…

At this point, I’m trying the following:

My request:

`
.body(StringBody("""{ “userName”:"${username}", “code”:null}""")).asJSON))

`

The response to this request is:

`
{“errorMessage”:null,“success”:true,“errorCode”:null}

`

What I want to do:
Call a function that stores ${username} into a file ONLY if response has “success”:true

What am I trying right now:

`

.body(StringBody("""{ “userName”:"${username}", “code”:null}""")).asJSON)).check(substring(“true”).saveAs(“success”))

.exec(session => {
if(session(“success”).as[String] == “true”){
writeUserName.writeToFile_Str(pathForUsername,session(“username”).as[String])
}

session
})

`

It’s not working for some reason

Hope it didn’t sound confusing…

Good Day,
M S Puranik