encodeBody static method

Uint8List encodeBody(
  1. dynamic body,
  2. String? contentType
)

Implementation

static Uint8List encodeBody(dynamic body, String? contentType) {
  if (body == null) return Uint8List(0);

  if (body is String) {
    return Uint8List.fromList(utf8.encode(body));
  }

  if (body is List<int>) {
    return Uint8List.fromList(body);
  }

  if (body is Uint8List) {
    return body;
  }

  if (body is Map<String, dynamic>) {
    if (contentType?.contains('application/json') == true) {
      return Uint8List.fromList(utf8.encode(jsonEncode(body)));
    } else {
      // Form data
      final formData = buildQueryString(body);
      return Uint8List.fromList(utf8.encode(formData));
    }
  }

  // Fallback to string conversion
  return Uint8List.fromList(utf8.encode(body.toString()));
}