formatBody static method

String formatBody(
  1. dynamic body,
  2. String? contentType
)

Implementation

static String formatBody(dynamic body, String? contentType) {
  try {
    if (body == null) {
      return _emptyBody;
    }

    var bodyContent = _emptyBody;

    if (contentType == null ||
        !contentType.toLowerCase().contains(_applicationJson)) {
      final bodyTemp = body.toString();

      if (bodyTemp.isNotEmpty) {
        bodyContent = bodyTemp;
      }
    } else {
      if (body is String && body.contains("\n")) {
        bodyContent = body;
      } else {
        if (body is String) {
          if (body.isNotEmpty) {
            //body is minified json, so decode it to a map and let the encoder pretty print this map
            bodyContent = _parseJson(_decodeJson(body));
          }
        } else if (body is Stream) {
          bodyContent = _stream;
        } else {
          bodyContent = _parseJson(body);
        }
      }
    }

    return bodyContent;
  } catch (exception) {
    return _parseFailedText + body.toString();
  }
}