# How to use doIf properly

**URL:** <https://community.gatling.io/t/how-to-use-doif-properly/6296>\
**Category:** Gatling (Open-Source)\
**Created:** [November 3, 2021, 3:50pm UTC](https://community.gatling.io/t/how-to-use-doif-properly/6296 "2021-11-03T15:50:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arditi\_One](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@Arditi\_One](https://community.gatling.io/u/Arditi_One)\
**Post date:** [November 3, 2021, 3:50pm UTC](https://community.gatling.io/t/how-to-use-doif-properly/6296/1 "2021-11-03T15:50:46Z")

</div>

Hi,

I have a scenarios where I have to upload an X file to the Y endpoint. The upload process usually takes more than 10mins roughly 13-17 minutes. I do verify the file uploaded successfully by calling the polling endpoint (every 10 mins).

The api security is implemented by Keycloak and after 5 mins my auth-token is expiring resulting the test to fail.

I need to add a condition to my API call chain that: “In case the endpoint returns 401 - Unauthorized do an extra call Z to generate a new auth token”.

Up until now I wasn’t able to do this. Below I will list some code I wrote and wait for some help.

val uploadFile: ChainBuilder = exec(securedPost(EndPoint.FileUpload)  
.headers(Headers.FileImportHeaders)  
.bodyPart(RequestBody.File).asMultipartForm  
.check(pollingUrl))  
.exec {  
\_.set(“uploadStatus”, “PENDING”)  
}  
.asLongAs(session =\> !session(“uploadStatus”).as[String].equals(“SUCCESS”) && !session(“uploadStatus”).as[String].equals(“WARNING”)) {  
exec(securedGet(“Poll file status”, “${pollingUrl}”) // here is where the 401 is thrown after 5 minutes  
.check(uploadStatus, submissionId, batchId)  
.check(status.saveAs(“statusCode”))  
)  
.doIf(session =\> session(“statusCode”).as[Int] == 401) {  
exec(generateNewTokenUsing(refreshToken))  
}  
.exitHereIfFailed.pause(PollingTimeout)  
}
