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

A Flutter package for logging curl with dio interceptor.

dio_curl_interceptor #

A Flutter package that provides a Dio interceptor for logging HTTP requests as cURL commands. This makes it easier to debug API calls and share them with others.

Screenshot

Features #

  • πŸ” Automatically converts Dio HTTP requests to cURL commands
  • πŸ“ Logs cURL commands to the console for easy debugging
  • βš™οΈ Configurable options for logging behavior
  • πŸ”„ Support for FormData conversion
  • 🎯 Minimal setup required

Getting started #

Add dio_curl_interceptor to your pubspec.yaml file:

dependencies:
  dio_curl_interceptor: ^1.0.0

Then run:

flutter pub get

Usage #

  • (1) First, import the package:
import 'package:dio_curl_interceptor/dio_curl_interceptor.dart';
  • (2) Add the interceptor to your Dio instance:

Simple Usage #

final dio = Dio();
dio.interceptors.add(CurlInterceptor());

Configuration Options #

You can customize the interceptor behavior with CurlOptions:

dio.interceptors.add(CurlInterceptor(
  curlOptions: CurlOptions(
    statusCode: true, // Show status codes in logs
    responseTime: true, // Show response timing
    convertFormData: true, // Convert FormData to JSON in cURL output
    onRequest: RequestDetails(visible: true),
    onResponse: ResponseDetails(visible: true, responseBody: true),
    onError: ErrorDetails(visible: true, responseBody: true),
    // Format response body with build-in formatters
    formatter: CurlFormatters.escapeNewlinesString,
  ),
  // Custom printer function to override default logging behavior
  printer: (String text) {
    // Your custom logging implementation
    print('Custom log: $text');
  },
));

Custom Printer Function #

The printer parameter allows you to override the default logging (from colored_logger) behavior:

dio.interceptors.add(CurlInterceptor(
  printer: (String text) {
    // Implement your own logging logic here
    // For example, log to a file, send to a remote service, or use a custom logger
    print('API Debug: $text');
  },
));

Handling Very Long Messages

For very long messages, consider using the log function from the developer package which handles message chunking automatically:

import 'dart:developer' as developer;

dio.interceptors.add(CurlInterceptor(
  printer: (String text) {
    // The developer.log function automatically handles very long messages
    developer.log(text, name: 'CURL');
  },
));

Built-in Formatters #

The package includes CurlFormatters with built-in formatting utilities:

  • escapeNewlinesString: Formats strings by escaping newlines
// Example usage
final formatted = CurlFormatters.escapeNewlinesString("Hello\nWorld");
// Output: "Hello\nWorld"
  • readableMap: Converts maps to a readable console format
// Example usage
final map = {
  'name': 'John',
  'details': 'Line 1\nLine 2'
};
final formatted = CurlFormatters.readableMap(map);
// Output:
// name: John
// details: Line 1\nLine 2

Example Output #

When making a GET request with JSON data, The interceptor will log something like:

Screenshot

Additional information #

  • Repository: GitHub
  • Bug Reports: Please file issues on the GitHub repository
  • Feature Requests: Feel free to suggest new features through GitHub issues

Contributions are welcome! Please feel free to submit a Pull Request.

2
likes
0
points
1.24k
downloads

Publisher

verified publishervenhdev.me

Weekly Downloads

A Flutter package for logging curl with dio interceptor.

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

colored_logger, dio, flutter

More

Packages that depend on dio_curl_interceptor