read static method

Future<RequestBody> read(
  1. HttpRequest request, [
  2. ContentType? contentType
])

contentType is the Content-Type of request when the caller already read it. Else it is read here.

Implementation

static Future<RequestBody> read(
  HttpRequest request, [
  ContentType? contentType,
]) async {
  contentType ??= request.headers.contentType;
  if (contentType == null) return RequestBody.empty();

  RequestBody? now = readNow(request, contentType);
  if (now != null) return now;

  if (HttpBody.isJson(contentType)) {
    return _json(await readBytes(request, Dox().config.maxBodySize));
  }

  if (HttpBody.isFormData(contentType)) {
    int max = Dox().config.maxMultipartSize;
    _checkContentLength(request, max);
    FormDataVisitor visitor = FormDataVisitor(
      request,
      limit(request, max),
      boundary: contentType.parameters['boundary'],
    );
    await visitor.process();
    return RequestBody(
      const FormInput().nested(visitor.inputs, () => visitor.parts),
      kind: BodyKind.multipart,
      parts: visitor.parts,
    );
  }

  return _urlEncoded(await readBytes(request, Dox().config.maxBodySize));
}