http_intercept

The http_intercept package provides a flexible and easy-to-use way to intercept and manipulate HTTP requests and responses in Dart and Flutter applications. It allows you to add custom behavior such as logging, authentication, and error handling to your HTTP requests.

Installation

For Dart

Run the following command:

dart pub add http_intercept

For Flutter

Run the following command:

flutter pub add http_intercept

Usage

To use the http_intercept package, you need to create an instance of HttpInterceptorWrapper and use it with your HTTP client. Here's a basic example:

For Dart

import 'dart:async';

import 'package:http/http.dart' as http;
import 'package:http_intercept/http_intercept.dart';

void main() {
  unawaited(
    http.runWithClient(
      _myDartApp,
      () => HttpClientProxy(
        interceptors: [
          HttpInterceptorWrapper(
            onRequest: (requestOptions) {
              // Add custom headers or modify the request
              requestOptions.headers['Authorization'] = 'Bearer YOUR_TOKEN';
              return OnRequest.next(requestOptions);
            },
          ),
        ],
      ),
    ),
  );
}

Future<void> _myDartApp() async {
  final client = http.Client();
  final response = await client.get(Uri.parse('https://api.example.com/data'));
  print(response.body);
}

For Flutter

It is similar with Flutter:

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:http_intercept/http_intercept.dart';

void main() {
  unawaited(
    http.runWithClient(
      () {
        runApp(const MyApp());
      },
      () => HttpClientProxy(
        interceptors: [
          HttpInterceptorWrapper(
            onRequest: (requestOptions) {
              // Add custom headers or modify the request
              requestOptions.headers['Authorization'] = 'Bearer YOUR_TOKEN';
              return OnRequest.next(requestOptions);
            },
          ),
        ],
      ),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    // add your code here
  }
}

Advanced Usage

For more advanced usage, you can chain multiple interceptors, handle specific error types, or modify responses before they reach your application. Here's an example:

import 'dart:async';

import 'package:http/http.dart' as http;
import 'package:http_intercept/http_intercept.dart';

class LoggingInterceptor extends HttpInterceptor {
  @override
  FutureOr<OnRequest> onRequest(http.BaseRequest request) {
    print('Request: ${request.method} ${request.url}');
    return OnRequest.next(request);
  }

  @override
  FutureOr<OnResponse> onResponse(http.StreamedResponse response) {
    print('Response: ${response.statusCode}');
    return OnResponse.next(response);
  }

  @override
  FutureOr<OnError> onError(
    http.BaseRequest request,
    Object error,
    StackTrace? stackTrace,
  ) {
    print('Error: $error');
    return OnError.next(request, error, stackTrace);
  }
}

void main() {
  unawaited(
    http.runWithClient(
      _myDartApp,
      () => HttpClientProxy(
        interceptors: [
          LoggingInterceptor(),
          // other interceptors
        ],
      ),
    ),
  );
}

Future<void> _myDartApp() async {
  final client = http.Client();
  final response = await client.get(Uri.parse('https://api.example.com/data'));
  print(response.body);
}

Additional Information

If you encounter any issues or have questions, feel free to open an issue on the GitHub repository.

Thank you for using http_intercept! We hope it makes your HTTP request handling easier and more flexible.

Libraries

http_intercept