# How to retry http().get() on a specific error code

**URL:** <https://community.gatling.io/t/how-to-retry-http-get-on-a-specific-error-code/4110>\
**Category:** Gatling (Open-Source)\
**Created:** [January 8, 2018, 1:51am UTC](https://community.gatling.io/t/how-to-retry-http-get-on-a-specific-error-code/4110 "2018-01-08T01:51:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Douglas\_Rosser](https://avatars.discourse-cdn.com/v4/letter/d/41988e/32.png) [@Douglas\_Rosser](https://community.gatling.io/u/Douglas_Rosser)\
**Post date:** [January 8, 2018, 1:51am UTC](https://community.gatling.io/t/how-to-retry-http-get-on-a-specific-error-code/4110/1 "2018-01-08T01:51:48Z")

</div>

I’m pretty new to Gatling, but I have a currently running test that I need to modify. I have a RESTful http endpoint that can either return the data I need, or it can return a 503 error with a number of seconds I should wait and then re-try. Any other error should be considered failure.

From the Gatling docs, it looks like looping and retries are implemented at the “scenario” level and not at the HTTP Request. I can live with that limitation, but I can’t figure out how to use check() or checkIf() to “break” from the following HTTP Requests and go back to the beginning of the scenario.

Any ideas?

Regards,  
dr

---

<div class="post-metadata">

**Author:** ![Douglas\_Rosser](https://avatars.discourse-cdn.com/v4/letter/d/41988e/32.png) [@Douglas\_Rosser](https://community.gatling.io/u/Douglas_Rosser)\
**Post date:** [January 9, 2018, 3:27am UTC](https://community.gatling.io/t/how-to-retry-http-get-on-a-specific-error-code/4110/2 "2018-01-09T03:27:55Z")

</div>

So I re-went through the tutorial again and this is sort of what I’m trying to accomplish. Of course, this code doesn’t compile, probably because I’m trying to use checkIf() in a way it wasn’t meant to be used…

class firstTest extends Simulation {

val httpProtocol = http  
.baseURL(“[http://httpstat.us](http://httpstat.us)”)  
.inferHtmlResources()  
.acceptHeader(“text/html,application/xhtml+xml,application/xml;q=0.9,_/_;q=0.8”)  
.acceptEncodingHeader(“gzip, deflate”)  
.acceptLanguageHeader(“en-US,en;q=0.5”)  
.userAgentHeader(“Mozilla/5.0 (X11; Ubuntu; Linux x86\_64; rv:57.0) Gecko/20100101 Firefox/57.0”)

val headers\_0 = Map(“Upgrade-Insecure-Requests” → “1”)

val scn = scenario(“firstTest”)  
.exec(http(“request\_0”)  
.get("/503")  
.checkIf(status.is(503)) {  
tryMax(5) {  
pause(5) //TODO get pause duration from the 503  
exec(http(“retry\_503”)  
.get("/503")  
}  
}  
.headers(headers\_0))

setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)  
}

---

<div class="post-metadata">

**Author:** ![Douglas\_Rosser](https://avatars.discourse-cdn.com/v4/letter/d/41988e/32.png) [@Douglas\_Rosser](https://community.gatling.io/u/Douglas_Rosser)\
**Post date:** [January 10, 2018, 4:32am UTC](https://community.gatling.io/t/how-to-retry-http-get-on-a-specific-error-code/4110/3 "2018-01-10T04:32:53Z")

</div>

Third try is the charm. I’m fairly certain “checkIf” doesn’t do what I thought it would do, but asLongAs() fits my use case. I need to fully test this, but I’m already wondering why the truth test doesn’t work correctly if I put quotes around 200. This is definitely not what I expect, as a bare 200 looks like an Int and not a String!

import scala.concurrent.duration.\_

import io.gatling.core.Predef.\_  
import io.gatling.http.Predef.\_  
import io.gatling.jdbc.Predef.\_

class firstTest extends Simulation {

object Retry {  
val retry = asLongAs(session =\> session(“ret\_code”).as[String] != 200) {  
pause(5) //TODO - use the duration returned from the endpoint  
.exec(http(“retrying”)  
.get("/")  
.check(status.in(200,503),status.saveAs(“ret\_code”), bodyString.saveAs(“ret\_body”))  
).exitHereIfFailed  
}  
}

val httpProtocol = http  
.baseURL(“[http://httpstat.us](http://httpstat.us)”)  
.inferHtmlResources()  
.acceptHeader(“text/html,application/xhtml+xml,application/xml;q=0.9,_/_;q=0.8”)  
.acceptEncodingHeader(“gzip, deflate”)  
.acceptLanguageHeader(“en-US,en;q=0.5”)  
.userAgentHeader(“Mozilla/5.0 (X11; Ubuntu; Linux x86\_64; rv:57.0) Gecko/20100101 Firefox/57.0”)

val headers\_0 = Map(“Upgrade-Insecure-Requests” → “1”)

val scn = scenario(“firstTest”)  
.exec(http(“request\_0”)  
.get("/503")  
.check(status.saveAs(“ret\_code”), bodyString.saveAs(“ret\_body”)) //TODO - instead of the body, get the duration to wait from the endpoint  
.headers(headers\_0)  
)  
.exec(Retry.retry)

setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)  
}
