read static method
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();
if (HttpBody.isJson(contentType)) {
Uint8List bytes = await readBytes(request, Dox().config.maxBodySize);
if (bytes.isEmpty) {
return RequestBody.empty();
}
try {
Object? data = _jsonUtf8.convert(bytes);
return RequestBody(
data is Map<String, dynamic> ? data : <String, dynamic>{},
kind: BodyKind.json,
json: data,
);
} catch (err) {
return RequestBody(<String, dynamic>{}, kind: BodyKind.invalidJson);
}
}
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(
visitor.inputs,
kind: BodyKind.multipart,
parts: visitor.parts,
);
}
if (HttpBody.isUrlEncoded(contentType)) {
Uint8List bytes = await readBytes(request, Dox().config.maxBodySize);
String text = utf8.decode(bytes, allowMalformed: true);
Map<String, dynamic> map = <String, dynamic>{};
try {
map = Uri.splitQueryString(text);
} catch (err) {
/// bad percent encoding: an empty body
}
return RequestBody(map, kind: BodyKind.urlEncoded, text: text);
}
return RequestBody(<String, dynamic>{}, kind: BodyKind.other);
}