tryJsonDecode function

Future<Map?> tryJsonDecode(
  1. Request request
)

Implementation

Future<Map?> tryJsonDecode(Request request) async {
  if (request.method == 'GET') return null;
  print(request.isMultipart);

  if (!request.isMultipart) {
    try {
      final data = await request.readAsString();
      return jsonDecode(data);
    } on FormatException catch (e) {
      if (e.message == 'Unexpected extension byte') {
      } else if (e.message == 'Missing expected digit') {}
      return null;
    }
  }

  await for (final part in request.parts) {
    var header = HeaderValue.parse(request.headers['content-type']!);
    if (part.headers.containsKey('content-disposition')) {
      header = HeaderValue.parse(part.headers['content-disposition']!);
      var filename = header.parameters['filename']!;
      final file = File(filename);
      final fileSink = file.openWrite();
      await part.pipe(fileSink);
      await fileSink.close();
    }
  }
}