formatBody static method

String formatBody({
  1. required BuildContext context,
  2. required dynamic body,
  3. String? contentType,
})

Formats body based on contentType. If body is null it will return _emptyBody. Otherwise if body type is json - it will try to format it.

Implementation

static String formatBody({
  required BuildContext context,
  required dynamic body,
  String? contentType,
}) {
  try {
    if (body == null) {
      return context.i18n(AliceTranslationKey.callRequestBodyEmpty);
    }

    String bodyContent =
        context.i18n(AliceTranslationKey.callRequestBodyEmpty);

    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 (_) {
    return context.i18n(AliceTranslationKey.parserFailed) + body.toString();
  }
}