You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In order to implement `http_interceptor` you need to implement the `InterceptorContract` and create your own interceptor. This abstract class has four methods:
71
72
72
-
-`interceptRequest`, which triggers before the http request is called
73
-
-`interceptResponse`, which triggers after the request is called, it has a response attached to it which the corresponding to said request;
74
-
75
-
-`shouldInterceptRequest` and `shouldInterceptResponse`, which are used to determine if the request or response should be intercepted or not. These two methods are optional as they return `true` by default, but they can be useful if you want to conditionally intercept requests or responses based on certain criteria.
73
+
-`interceptRequest`, which triggers before the http request is called
74
+
-`interceptResponse`, which triggers after the request is called, it has a response attached to it which the corresponding to said request;
75
+
76
+
-`shouldInterceptRequest` and `shouldInterceptResponse`, which are used to determine if the request or response should be intercepted or not. These two methods are optional as they return `true` by default, but they can be useful if you want to conditionally intercept requests or responses based on certain criteria.
76
77
77
78
You could use this package to do logging, adding headers, error handling, or many other cool stuff. It is important to note that after you proccess the request/response objects you need to return them so that `http` can continue the execute.
78
79
@@ -89,33 +90,41 @@ class LoggerInterceptor extends InterceptorContract {
89
90
print('----- Request -----');
90
91
print(request.toString());
91
92
print(request.headers.toString());
93
+
print('Request type: ${request.runtimeType}');
92
94
return request;
93
95
}
94
96
95
97
@override
96
98
BaseResponse interceptResponse({
97
99
required BaseResponse response,
98
100
}) {
99
-
log('----- Response -----');
100
-
log('Code: ${response.statusCode}');
101
+
print('----- Response -----');
102
+
print('Code: ${response.statusCode}');
103
+
print('Response type: ${response.runtimeType}');
101
104
if (response is Response) {
102
-
log((response).body);
105
+
print((response).body);
103
106
}
104
107
return response;
105
108
}
106
109
}
107
110
```
108
111
109
-
- Changing headers with interceptor:
112
+
- Changing headers and query parameters with interceptor:
110
113
111
114
```dart
112
115
class WeatherApiInterceptor implements InterceptorContract {
print('Response body size: ${bytes.length} bytes');
417
+
418
+
// Example: decode binary data
419
+
if (bytes.isNotEmpty) {
420
+
final decoded = utf8.decode(bytes);
421
+
print('Decoded response: $decoded');
422
+
}
423
+
}
424
+
return response;
425
+
}
426
+
}
427
+
```
428
+
344
429
### Using self signed certificates
345
430
346
431
You can achieve support for self-signed certificates by providing `InterceptedHttp` or `InterceptedClient` with the `client` parameter when using the `build` method on either of those, it should look something like this:
@@ -377,12 +462,6 @@ final http = InterceptedHttp.build(
377
462
378
463
_**Note:** It is important to know that since both HttpClient and IOClient are part of `dart:io` package, this will not be a feature that you can perform on Flutter Web (due to `BrowserClient` and browser limitations)._
379
464
380
-
## Roadmap
381
-
382
-
Check out our roadmap [here](https://doc.clickup.com/p/h/82gtq-119/f552a826792c049).
383
-
384
-
_We migrated our roadmap to better suit the needs for development since we use ClickUp as our task management tool._
385
-
386
465
## Troubleshooting
387
466
388
467
Open an issue and tell me, I will be happy to help you out as soon as I can.
0 commit comments