|
| 1 | +# Http Inspector |
| 2 | + |
| 3 | +[](https://pub.dev/packages/http_inspector) |
| 4 | +[](LICENSE) |
| 5 | +[](https://flutter.dev) |
| 6 | +[](https://pub.dev/packages/dio) |
| 7 | + |
| 8 | +**语言**:[English](README.md) · **简体中文** |
| 9 | + |
| 10 | +轻量级 Flutter 应用内 HTTP 抓包工具,专为 Dio 打造。实时捕获请求、响应与错误,内置可视化 UI、JSON 美化、cURL 一键导出、搜索过滤 —— 让调试网络请求这件事不用再离开 App。 |
| 11 | + |
| 12 | +<img src="assets/screenshots/screenshot_1.png" height="400" /> |
| 13 | +<img src="assets/screenshots/screenshot_2.png" height="400" /> |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## 为什么用它 |
| 18 | + |
| 19 | +在 Flutter 项目里调试网络请求,你可能遇到过: |
| 20 | + |
| 21 | +- 用 Charles / Proxyman 抓包,但真机上配代理太麻烦,HTTPS 还要装证书 |
| 22 | +- 打 `print(response.data)`,日志被淹没在其他输出里 |
| 23 | +- 想复现 Bug,得手动复制 URL、Header、Body 拼 cURL |
| 24 | + |
| 25 | +`http_inspector` 直接在 App 内部打开一个「网络面板」,能看到所有请求详情、复制 cURL 交给后端复现、也能在离线设备上快速定位问题。**装在 Dio 上一行代码就跑起来。** |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## 功能特性 |
| 30 | + |
| 31 | +- 🔴 **实时日志** —— 请求、响应、错误全捕获,带时间戳和耗时 |
| 32 | +- 📱 **应用内查看** —— `HttpScopeView` 直接嵌入 App,无需切走 |
| 33 | +- 📋 **cURL 导出** —— 任意请求一键复制成可运行的 cURL 命令 |
| 34 | +- 🎨 **JSON 美化** —— 请求/响应体和 Header 格式化展示 |
| 35 | +- 🔍 **搜索过滤** —— 按 URL、域名、关键字定位请求 |
| 36 | +- 🌈 **彩色控制台** —— 可配置的彩色日志,扫一眼就知道成败 |
| 37 | +- 🔒 **生产环境安全** —— 用 `kDebugMode` 守护,避免泄漏敏感数据 |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## 安装 |
| 42 | + |
| 43 | +在 `pubspec.yaml` 中添加: |
| 44 | + |
| 45 | +```yaml |
| 46 | +dependencies: |
| 47 | + http_inspector: ^1.0.2 |
| 48 | +``` |
| 49 | +
|
| 50 | +然后执行: |
| 51 | +
|
| 52 | +```bash |
| 53 | +flutter pub get |
| 54 | +``` |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## 快速上手 |
| 59 | + |
| 60 | +### 第 1 步 —— 给 Dio 挂上拦截器 |
| 61 | + |
| 62 | +```dart |
| 63 | +import 'package:dio/dio.dart'; |
| 64 | +import 'package:http_inspector/http_inspector.dart'; |
| 65 | +
|
| 66 | +final dio = Dio(); |
| 67 | +
|
| 68 | +dio.interceptors.add( |
| 69 | + HttpInspectorInterceptor( |
| 70 | + options: const FancyDioInspectorOptions( |
| 71 | + consoleOptions: FancyDioInspectorConsoleOptions(verbose: true), |
| 72 | + ), |
| 73 | + ), |
| 74 | +); |
| 75 | +``` |
| 76 | + |
| 77 | +### 第 2 步 —— 把面板挂到 App 上 |
| 78 | + |
| 79 | +```dart |
| 80 | +import 'package:flutter/foundation.dart'; |
| 81 | +
|
| 82 | +MaterialApp( |
| 83 | + home: Scaffold( |
| 84 | + // 方式 A:从右侧抽屉呼出(仅 debug 构建) |
| 85 | + endDrawer: kDebugMode ? const FancyDioInspectorView() : null, |
| 86 | +
|
| 87 | + // 方式 B:手动跳转 |
| 88 | + body: ElevatedButton( |
| 89 | + onPressed: () => Navigator.of(context).push( |
| 90 | + MaterialPageRoute(builder: (_) => const HttpScopeView()), |
| 91 | + ), |
| 92 | + child: const Text('打开抓包面板'), |
| 93 | + ), |
| 94 | + ), |
| 95 | +); |
| 96 | +``` |
| 97 | + |
| 98 | +搞定 —— 现在跑一次网络请求,就能在面板里看到详情。 |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +## 进阶用法 |
| 103 | + |
| 104 | +### 拦截器完整配置 |
| 105 | + |
| 106 | +```dart |
| 107 | +HttpInspectorInterceptor( |
| 108 | + options: const FancyDioInspectorOptions( |
| 109 | + maxLogs: 200, // 最多保留多少条日志 |
| 110 | + consoleOptions: FancyDioInspectorConsoleOptions( |
| 111 | + verbose: true, // 打印详细日志到控制台 |
| 112 | + colorize: true, // 彩色输出 |
| 113 | + ), |
| 114 | + ), |
| 115 | + onRequestCreated: (requestOptions) { |
| 116 | + // 请求发出前的钩子(打点、埋点、修改参数等) |
| 117 | + }, |
| 118 | + onResponseCreated: (response) { |
| 119 | + // 响应回来后的钩子 |
| 120 | + }, |
| 121 | + onErrorCreated: (dioError) { |
| 122 | + // 错误发生时的钩子(上报到 Sentry / Firebase 等) |
| 123 | + }, |
| 124 | +) |
| 125 | +``` |
| 126 | + |
| 127 | +### 代码里直接拿 cURL |
| 128 | + |
| 129 | +```dart |
| 130 | +final curl = requestOptions.cURL; |
| 131 | +// 现在可以打日志、发给后端、写到剪贴板等等 |
| 132 | +``` |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## 示例项目 |
| 137 | + |
| 138 | +仓库自带 `example/`,直接跑: |
| 139 | + |
| 140 | +```bash |
| 141 | +cd example |
| 142 | +flutter pub get |
| 143 | +flutter run |
| 144 | +``` |
| 145 | + |
| 146 | +--- |
| 147 | + |
| 148 | +## API 速查 |
| 149 | + |
| 150 | +| 名称 | 类型 | 说明 | |
| 151 | +|------|------|------| |
| 152 | +| `HttpInspectorInterceptor` | Interceptor | 挂到 Dio 上捕获流量 | |
| 153 | +| `FancyDioInspectorOptions` | Options | 配置日志上限、控制台输出等 | |
| 154 | +| `FancyDioInspectorView` | Widget | 全屏抓包面板 | |
| 155 | +| `HttpScopeView` | Widget | 轻量版抓包面板 | |
| 156 | +| `NetworkRequestModel` | Model | 请求数据模型 | |
| 157 | +| `NetworkResponseModel` | Model | 响应数据模型 | |
| 158 | +| `NetworkErrorModel` | Model | 错误数据模型 | |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +## 隐私与生产环境 |
| 163 | + |
| 164 | +- **只在 debug 构建里启用面板** —— 用 `kDebugMode` 或自己的 flag 守护 |
| 165 | +- **不要记录敏感数据** —— token、密码、身份证号等要在拦截器里脱敏 |
| 166 | +- 日志存在内存中,`maxLogs` 控制上限,不会写盘 |
| 167 | + |
| 168 | +--- |
| 169 | + |
| 170 | +## 兼容性 |
| 171 | + |
| 172 | +| 依赖 | 版本 | |
| 173 | +|------|------| |
| 174 | +| Dart | >= 2.17.6, < 4.0.0 | |
| 175 | +| Flutter | >= 3.0.5 | |
| 176 | +| Dio | ^5.x | |
| 177 | + |
| 178 | +--- |
| 179 | + |
| 180 | +## 贡献 |
| 181 | + |
| 182 | +1. Fork 仓库,创建 feature 分支 |
| 183 | +2. 遵循现有代码风格进行修改 |
| 184 | +3. 提交前跑一遍检查: |
| 185 | + |
| 186 | +```bash |
| 187 | +flutter format . |
| 188 | +flutter analyze |
| 189 | +flutter test |
| 190 | +``` |
| 191 | + |
| 192 | +4. 提 PR,描述清楚改动动机与影响 |
| 193 | + |
| 194 | +详见 [CONTRIBUTING.md](CONTRIBUTING.md)。 |
| 195 | + |
| 196 | +--- |
| 197 | + |
| 198 | +## 协议 |
| 199 | + |
| 200 | +MIT —— 见 [LICENSE](LICENSE)。 |
| 201 | + |
| 202 | +## 更新日志 |
| 203 | + |
| 204 | +见 [CHANGELOG.md](CHANGELOG.md)。 |
0 commit comments