readAsString method

Future<String> readAsString({
  1. Encoding? encoding,
  2. int? maxLength,
})

Reads the body as a string, decoding it using the specified or detected encoding. Defaults to utf8 if no encoding is provided or detected.

Example

router.post('/api/data', (req) async {
  final body = await req.readAsString();
  final data = jsonDecode(body);
  // Use data...
});

Implementation

Future<String> readAsString({Encoding? encoding, final int? maxLength}) {
  encoding ??= body.bodyType?.encoding ?? utf8;
  return encoding.decodeStream(read(maxLength: maxLength));
}