body method

String body(
  1. String value, {
  2. String? contentType,
})

Implementation

String body(String value, {String? contentType}) {
  final mediaType = contentType?.split(';').first.trim().toLowerCase();
  if (mediaType?.startsWith('multipart/') == true) return masked;

  final trimmed = value.trimLeft();
  if (mediaType?.contains('json') == true ||
      trimmed.startsWith('{') ||
      trimmed.startsWith('[')) {
    try {
      return jsonEncode(_value(jsonDecode(value), depth: 0));
    } on FormatException {
      // A partial payload still receives the bounded textual pass below.
    }
  }
  if (mediaType == 'application/x-www-form-urlencoded') {
    try {
      final parameters = Uri(query: value).queryParametersAll;
      return Uri(
        queryParameters: <String, Object?>{
          for (final entry in parameters.entries)
            entry.key: isSensitiveName(entry.key)
                ? <String>[for (final _ in entry.value) masked]
                : <String>[for (final item in entry.value) text(item)],
        },
      ).query;
    } on FormatException {
      // A partial form still receives the textual pass below.
    }
  }
  return text(value);
}