Interceptors and Logging
Interceptors are a powerful feature in network programming that allow you to intercept, modify, or monitor HTTP requests and responses before they reach your application logic or after they leave your app. In Flutter, interceptors are commonly used to automatically add headers (such as authentication tokens), handle errors in a unified way, and log requests and responses for debugging. By integrating interceptors, you can ensure that every request sent from your app contains the necessary headers, and you can capture important information about network activity without scattering logging code throughout your project.
main.dart
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import 'dart:convert'; import 'package:http/http.dart' as http; class LoggingHttpClient extends http.BaseClient { final http.Client _inner; final Map<String, String> _defaultHeaders; LoggingHttpClient(this._inner, this._defaultHeaders); @override Future<http.StreamedResponse> send(http.BaseRequest request) async { // Add default headers request.headers.addAll(_defaultHeaders); // Log outgoing request print('Request: [1m${request.method} ${request.url}[0m'); print('Headers: ${request.headers}'); if (request is http.Request && request.body.isNotEmpty) { print('Body: ${request.body}'); } final response = await _inner.send(request); // Log response response.stream.bytesToString().then((body) { print('Response status: ${response.statusCode}'); print('Response body: $body'); }); return response; } } void main() async { final client = LoggingHttpClient( http.Client(), { 'Authorization': 'Bearer your_token_here', 'Content-Type': 'application/json', }, ); // Example GET request final response = await client.get(Uri.parse('https://jsonplaceholder.typicode.com/posts/1')); print('Final response status: ${response.statusCode}'); }
By using interceptors like the one above, you eliminate the need to manually add headers or logging statements to every network call in your app. This centralization not only reduces repetition but also makes it much easier to maintain and debug your code. If you ever need to update authentication logic or adjust logging, you only have to change it in one place, and the changes will apply to all requests automatically.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 6.67
Interceptors and Logging
Swipe to show menu
Interceptors are a powerful feature in network programming that allow you to intercept, modify, or monitor HTTP requests and responses before they reach your application logic or after they leave your app. In Flutter, interceptors are commonly used to automatically add headers (such as authentication tokens), handle errors in a unified way, and log requests and responses for debugging. By integrating interceptors, you can ensure that every request sent from your app contains the necessary headers, and you can capture important information about network activity without scattering logging code throughout your project.
main.dart
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import 'dart:convert'; import 'package:http/http.dart' as http; class LoggingHttpClient extends http.BaseClient { final http.Client _inner; final Map<String, String> _defaultHeaders; LoggingHttpClient(this._inner, this._defaultHeaders); @override Future<http.StreamedResponse> send(http.BaseRequest request) async { // Add default headers request.headers.addAll(_defaultHeaders); // Log outgoing request print('Request: [1m${request.method} ${request.url}[0m'); print('Headers: ${request.headers}'); if (request is http.Request && request.body.isNotEmpty) { print('Body: ${request.body}'); } final response = await _inner.send(request); // Log response response.stream.bytesToString().then((body) { print('Response status: ${response.statusCode}'); print('Response body: $body'); }); return response; } } void main() async { final client = LoggingHttpClient( http.Client(), { 'Authorization': 'Bearer your_token_here', 'Content-Type': 'application/json', }, ); // Example GET request final response = await client.get(Uri.parse('https://jsonplaceholder.typicode.com/posts/1')); print('Final response status: ${response.statusCode}'); }
By using interceptors like the one above, you eliminate the need to manually add headers or logging statements to every network call in your app. This centralization not only reduces repetition but also makes it much easier to maintain and debug your code. If you ever need to update authentication logic or adjust logging, you only have to change it in one place, and the changes will apply to all requests automatically.
Thanks for your feedback!