Handle block responses from the server
v3.0
When the "Automatic Interceptor" is enabled, the SDK will generate an error response when the URL request was blocked or when the challenge was solved/cancelled. You should check the response's body if it's one of the SDK's errors.
Checking the error
You can check the error by using those APIs:
PerimeterX/isRequestBlockedError(error:)
- returnstrue
when the request was blocked.PerimeterX/isChallengeSolvedError(error:)
- returnstrue
when the request was blocked and the user solved the challenge.PerimeterX/isChallengeCancelledError(error:)
- returnstrue
when the request was blocked and the user cancelled the challenge.
Here is an example on how to check the error from the response's body:
try {
val request: Request = Request.Builder().url("https://my-url.com").build()
okHttpClient.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
response.body?.string()?.let { responseBody ->
if (PerimeterX.isRequestBlockedError(responseBody)) {
println("request was blocked by PX")
}
if (PerimeterX.isChallengeSolvedError(responseBody)) {
println("request was blocked by PX and user solved the challenge")
}
if (PerimeterX.isChallengeCancelledError(responseBody)) {
println("request was blocked by PX and challenge was cancelled")
}
}
}
else {
println("request was finished")
}
}
} catch (exception: Exception) {
println("request was failed. exception: $exception")
}
When will you receive those errors
When the "Automatic Interceptor" is enabled, the SDK will handle the block response from your server. The SDK will return the following errors to your URL request's handler:
- "Request was blocked" - The URL request's handler will be called once the block response arrives to your app. A challenge will be presented to the user.
- "Challenge was solved" - The URL request's handler will be called once the challenge was solved by the user. You may retry the URL request after getting this error.
- "Challenge was cancelled" - The URL request'a handler will be called once the challenge was cancelled by the user.
How the SDK presents the block page to the user
When the SDK receives the block response from the server it performs the following actions:
- Presenting a
block activity
in the app, over your other activities. - In the
block activity
the SDK presents a web view which loads the challenge that must be solved by the user (or the bot). - Once the challenge is solved, the SDK removes the
block activity
and allow the user to continue working in your app.
How to handle "challenge was canclled"
In Android, there are few cases which the challenge will be cancelled:
- The user press on the device's physical back button.
- Other activity in your app is starting (which caused the challenge activity to be paused).
After the challenge is cancelled, the user will have to perform the action again. Then, a new challenge will appear. On both iOS and Android, your app should handle the "challenge was cancelled" result.
Updated 12 days ago