Token Refresh

Hi All, I am new to Gatling and scala. I am trying to execute a scenario for 2 hours and after 1 hour I am getting a 401 as my token goes expired.

Is it a way to check the token is active or is there any way forward? appreciated your help for this.

thanks

Hi,

You can do the following:

  1. Add check to the response which returns 401 eventually, something like this:
    .check(status.saveAs(“lastResponseStatus”)
  2. .doIf(session => session(“lastResponseStatus”).as[Int] == 401)
    {
    // authenticate again
    }

Thank you for the reply. I did manage to create the function. But I want to execute the authentication method once in 1 hour when my scenario is executing say 5 hours. I dont want to kill my token server [which is shared with plenty of apps]. Can I execute the method authenticate only once. My method is like this…

Thanks in Advance!!

Hello, have you managed to resolve your problem?
I have the same situation with token expiration, and need your help)

понедельник, 9 ноября 2020 г. в 23:31:30 UTC+2, diju....@gmail.com:

You could proactively refresh your token before it expires, the same way you’ve loaded it in the first place.

Yes. Sorry for the late reply. Recently I have managed to find a solution for this. We can add the token into

def setToken: Expression[Session] = session => {
val accessToken = gettoken();
session.set(“token”,setToken)
}

Once we have this, we can add this into a loop.

If also synchronize this call, it wont be kill the token generator. Hope this helps

Hi @ Diju_Elias, I am facing the same problem, Can you please elaborate a bit?
I wanted to make a post call, and i want to refresh token fro every 30 mins, Below is what i tried.

I tried to use pace - to call token method for every 30 mins like this , but pace doesn’t execute for every 30 mins, I was expecting pace to execute once as the method starts and execute next token method only after 30 mins, but it executes one after the other as the method starts.

private def Submit(noOfRuns: Int, bidSlots_feeder: BatchableFeederBuilder[String], round: String): ChainBuilder = {
pause(pauseBy(5) seconds)
.repeat(noOfRuns) {
pace(3600 seconds). —> want to run token method to set token in the session for every 30 mins.
.exec(AuthRequest.auth_token)
.feed(order_feeder)
.doIfEqualsOrElse(round, “1”) {
pause(pauseBy(10) seconds)
.exec(postToUri(s"${config.base}/orders/#{$AuctionId}/base-order/proxy-order", s"Submit orders for : #{$OrderId}")
.queryParam(“employeeId”, “#{empNo}”)
.body(StringBody(session => {
// body
}))
)
}

//postToUri uses the access token from the session for the post call.

Can you please advise the solutions you have implemented and how to use that to refresh the token?

Can you please advise where you are refreshing the token here ?

def setToken: Expression[Session] = session => {
val accessToken = gettoken();
session.set(“token”,setToken)
}

Once we have this, we can add this into a loop.

Can you please advise the solutions that was implemented here and how token is refreshed here before it expires in the block below?