|
| 1 | +import 'dart:convert'; |
| 2 | + |
1 | 3 | import 'package:dio/dio.dart'; |
2 | 4 | import 'package:instabug_flutter/instabug_flutter.dart'; |
3 | 5 |
|
@@ -78,31 +80,60 @@ class InstabugDioInterceptor extends Interceptor { |
78 | 80 | requestBodySize = |
79 | 81 | int.parse(response.requestOptions.headers['content-length'] ?? '0'); |
80 | 82 | } else if (response.requestOptions.data != null) { |
81 | | - requestBodySize = response.requestOptions.data.toString().length; |
| 83 | + // Calculate actual byte size for more accurate size estimation |
| 84 | + requestBodySize = _calculateBodySize(response.requestOptions.data); |
82 | 85 | } |
83 | 86 |
|
84 | 87 | var responseBodySize = 0; |
85 | 88 | if (responseHeaders.containsKey('content-length')) { |
86 | 89 | // ignore: avoid_dynamic_calls |
87 | 90 | responseBodySize = int.parse(responseHeaders['content-length'][0] ?? '0'); |
88 | 91 | } else if (response.data != null) { |
89 | | - responseBodySize = response.data.toString().length; |
| 92 | + // Calculate actual byte size for more accurate size estimation |
| 93 | + responseBodySize = _calculateBodySize(response.data); |
90 | 94 | } |
91 | 95 |
|
92 | 96 | return data.copyWith( |
93 | 97 | endTime: endTime, |
94 | 98 | duration: endTime.difference(data.startTime).inMicroseconds, |
95 | 99 | url: response.requestOptions.uri.toString(), |
96 | 100 | method: response.requestOptions.method, |
97 | | - requestBody: response.requestOptions.data.toString(), |
| 101 | + requestBody: parseBody(response.requestOptions.data), |
98 | 102 | requestHeaders: response.requestOptions.headers, |
99 | 103 | requestContentType: response.requestOptions.contentType, |
100 | 104 | requestBodySize: requestBodySize, |
101 | 105 | status: response.statusCode, |
102 | | - responseBody: response.data.toString(), |
| 106 | + responseBody: parseBody(response.data), |
103 | 107 | responseHeaders: responseHeaders, |
104 | 108 | responseContentType: responseContentType, |
105 | 109 | responseBodySize: responseBodySize, |
106 | 110 | ); |
107 | 111 | } |
| 112 | + |
| 113 | + String parseBody(dynamic data) { |
| 114 | + try { |
| 115 | + return jsonEncode(data); |
| 116 | + } catch (e) { |
| 117 | + return data.toString(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /// Calculates the actual byte size of the body data |
| 122 | + int _calculateBodySize(dynamic data) { |
| 123 | + if (data == null) return 0; |
| 124 | + |
| 125 | + try { |
| 126 | + // For string data, get UTF-8 byte length |
| 127 | + if (data is String) { |
| 128 | + return data.codeUnits.length; |
| 129 | + } |
| 130 | + |
| 131 | + // For other types, try to encode as JSON and get byte length |
| 132 | + final jsonString = jsonEncode(data); |
| 133 | + return jsonString.codeUnits.length; |
| 134 | + } catch (e) { |
| 135 | + // Fallback to string conversion if JSON encoding fails |
| 136 | + return data.toString().codeUnits.length; |
| 137 | + } |
| 138 | + } |
108 | 139 | } |
0 commit comments