dio_curl_interceptor 1.1.0
dio_curl_interceptor: ^1.1.0 copied to clipboard
A Flutter package for logging curl with dio interceptor with Ansi escape codes makes it easy to debug.
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.
Features #
- π Automatically converts Dio HTTP requests to cURL commands
- π Logs cURL commands to the console with custom styles and printer
- βοΈ Configurable options for logging behavior
- π Support for FormData conversion
- π§° Standalone utility methods for custom interceptors
- π οΈ Direct utility functions without requiring the full interceptor
Getting started #
Add dio_curl_interceptor
to your pubspec.yaml
file:
dependencies:
dio_curl_interceptor: ^1.0.1
Then run:
flutter pub get
Usage #
Import the package #
import 'package:dio_curl_interceptor/dio_curl_interceptor.dart';
Option 1: Using the CurlInterceptor #
Add the interceptor to your Dio instance:
final dio = Dio();
dio.interceptors.add(CurlInterceptor());
You can customize the interceptor behavior with CurlOptions
:
dio.interceptors.add(CurlInterceptor(
curlOptions: CurlOptions(
status: 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 built-in formatters
formatter: CurlFormatters.escapeNewlinesString,
),
// Custom printer function to override default logging behavior
printer: (String text) {
// Your custom logging implementation
print('Custom log: $text');
},
));
Option 2: Using CurlUtils directly in your own interceptor #
If you prefer to use the utility methods in your own custom interceptor, you can use CurlUtils
directly:
class MyCustomInterceptor extends Interceptor {
final CurlOptions curlOptions;
final Map<RequestOptions, Stopwatch> _stopwatches = {};
MyCustomInterceptor({this.curlOptions = const CurlOptions()});
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
// Generate and log curl command
CurlUtils.logCurl(options, curlOptions: curlOptions);
// Add timing header if you want to track response time
CurlUtils.addXClientTime(options);
if (curlOptions.responseTime) {
final stopwatch = Stopwatch()..start();
_stopwatches[options] = stopwatch;
}
handler.next(options);
}
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
Stopwatch? stopwatch;
if (curlOptions.responseTime) {
stopwatch = _stopwatches.remove(response.requestOptions);
stopwatch?.stop();
}
// Handle and log response
CurlUtils.handleOnResponse(
response,
curlOptions: curlOptions,
stopwatch: stopwatch,
);
handler.next(response);
}
@override
void onError(DioException err, ErrorInterceptorHandler handler) {
Stopwatch? stopwatch;
if (curlOptions.responseTime) {
stopwatch = _stopwatches.remove(err.requestOptions);
stopwatch?.stop();
}
// Handle and log error
CurlUtils.handleOnError(
err,
curlOptions: curlOptions,
stopwatch: stopwatch,
);
handler.next(err);
}
}
Option 3: Using utility functions directly #
If you don't want to add a full interceptor, you can use the utility functions directly in your code:
// Generate a curl command from request options
final dio = Dio();
final response = await dio.get('https://example.com');
// Generate and log a curl command
CurlUtils.logCurl(response.requestOptions);
// Log response details
CurlUtils.handleOnResponse(response);
// Log error details
try {
await dio.get('https://invalid-url.com');
} on DioException catch (e) {
CurlUtils.handleOnError(e);
}
Additional Configuration #
You can customize the output with options:
CurlUtils.logCurl(
response.requestOptions,
curlOptions: CurlOptions(
convertFormData: true,
formatter: CurlFormatters.prettyJson,
),
);
License #
This project is licensed under the MIT License - see the LICENSE file for details.
- 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.