prettyFormatBody function

String prettyFormatBody(
  1. Object? data
)

Formats a request or response body as indented JSON; bytes and FormData get a short summary, and other text is returned as is.

Example:

final text = prettyFormatBody(response.data);
prettyFormatBody('{"id":7}'); // '{\n  "id": 7\n}'
prettyFormatBody('<html></html>'); // '<html></html>', not JSON so unchanged

Implementation

String prettyFormatBody(Object? data) {
  if (data == null) return 'null';
  if (data is Uint8List) return _formatBytes(data);
  if (data is FormData) return _formatFormData(data);
  try {
    final value = data is String ? jsonDecode(data) : data;
    return const JsonEncoder.withIndent('  ').convert(value);
  } catch (_) {
    return data.toString();
  }
}