Skip to content

Commit 3767b3b

Browse files
authored
Merge pull request #160 from KevinnZou/feature/mkdocs
feat:doc for request interception
2 parents 33cebd0 + 5caa338 commit 3767b3b

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

docs/interception.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Request Interception
2+
3+
Starting from version 1.9.8, this library provides a `RequestInterceptor` to allow developers to
4+
intercept the request and modify the request headers. It only supports the Android and iOS platform
5+
for now.
6+
7+
## RequestInterceptor
8+
9+
```kotlin
10+
/**
11+
* Interface for intercepting requests in WebView.
12+
*/
13+
interface RequestInterceptor {
14+
fun onInterceptUrlRequest(
15+
request: WebRequest,
16+
navigator: WebViewNavigator,
17+
): WebRequestInterceptResult
18+
}
19+
```
20+
21+
The `onInterceptUrlRequest` method will be called when the WebView sends a request.
22+
23+
## Sample
24+
25+
Developers can implement the `RequestInterceptor` interface to define their own interceptor.
26+
Then they can pass it to the `rememberWebViewNavigator` method to intercept the request.
27+
28+
```kotlin
29+
val navigator =
30+
rememberWebViewNavigator(
31+
requestInterceptor =
32+
object : RequestInterceptor {
33+
override fun onInterceptUrlRequest(
34+
request: WebRequest,
35+
navigator: WebViewNavigator,
36+
): WebRequestInterceptResult {
37+
return if (request.url.contains("kotlin")) {
38+
WebRequestInterceptResult.Modify(
39+
WebRequest(
40+
url = "https://kotlinlang.org/docs/multiplatform.html",
41+
headers = mutableMapOf("info" to "test"),
42+
),
43+
)
44+
} else {
45+
WebRequestInterceptResult.Allow
46+
}
47+
}
48+
},
49+
)
50+
```

0 commit comments

Comments
 (0)