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 two methods: `interceptRequest`, which triggers before the http request is called; and `interceptResponse`, which triggers after the request is called, it has a response attached to it which the corresponding to said request. You could use this 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.
71
71
72
+
`interceptRequest` and `interceptResponse` use `FutureOr` syntax, which makes it easier to support both synchronous and asynchronous behaviors.
73
+
72
74
- Logging with interceptor:
73
75
74
76
```dart
75
77
class LoggerInterceptor extends InterceptorContract {
76
78
@override
77
-
Future<BaseRequest> interceptRequest({
79
+
BaseRequest interceptRequest({
78
80
required BaseRequest request,
79
-
}) async {
81
+
}) {
80
82
print('----- Request -----');
81
83
print(request.toString());
82
84
print(request.headers.toString());
83
85
return request;
84
86
}
85
87
86
88
@override
87
-
Future<BaseResponse> interceptResponse({
89
+
BaseResponse interceptResponse({
88
90
required BaseResponse response,
89
-
}) async {
91
+
}) {
90
92
log('----- Response -----');
91
93
log('Code: ${response.statusCode}');
92
94
if (response is Response) {
@@ -102,7 +104,7 @@ class LoggerInterceptor extends InterceptorContract {
102
104
```dart
103
105
class WeatherApiInterceptor implements InterceptorContract {
0 commit comments