http_intercept 1.0.0 copy "http_intercept: ^1.0.0" to clipboard
http_intercept: ^1.0.0 copied to clipboard

A Dart package for intercepting and modifying HTTP requests and responses, useful for logging, authentication, and error handling.

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.

0
likes
150
pub points
11%
popularity

Publisher

verified publisherdasralph.de

A Dart package for intercepting and modifying HTTP requests and responses, useful for logging, authentication, and error handling.

Repository (GitHub)
View/report issues

Topics

#http #interceptor #logging #authentication #error-handling

Documentation

API reference

License

MIT (license)

Dependencies

collection, http, meta

More

Packages that depend on http_intercept