extractBody method

Future<Request> extractBody()

Extracts the request body (form-data / json / urlencoded) into body and prepares the merged view of body+query+params.

Idempotent: only the first call reads the socket. CSRF middleware can materialise the _csrf/_token fields early and the request handler can still call it afterwards without consuming the stream twice.

Implementation

Future<Request> extractBody() async {
  if (_bodyExtracted) return this;
  _bodyExtracted = true;
  final method = request.method.toLowerCase();
  if (method == 'post' ||
      method == 'patch' ||
      method == 'put' ||
      method == 'delete' ||
      method == 'query') {
    final isQuery = method == 'query';
    if (isQuery) _checkQueryMediaType();
    final content = await RequestBody.extract(
      request: request,
      strict: isQuery,
    );
    body = content.fields;
    _rawBody = content.bytes;
    // Anything read before now saw no body; rebuild on next use.
    _allData = null;
  }
  return this;
}