formData method
Returns a promise that resolves with a FormData representation of the request body.
Implementation
Future<FormData> formData() async {
throwIfBodyUsed();
_storage[#bodyUsed] = true;
final FormData? existing = _storage[#fromData];
if (existing != null) return existing;
// If content-type is `application/x-www-form-urlencoded` then parse the
// body as URLSearchParams.
if (headers
.get('content-type')
?.startsWith('application/x-www-form-urlencoded') ==
true) {
final params = URLSearchParams(await text());
final formData = FormData();
for (final (name, value) in params.entries()) {
formData.append(name, value);
}
return _storage[#fromData] = formData;
}
return switch (body) {
Stream<Uint8List> stream => await FormData.decode(
stream, _storage[#boundary] ?? headers.multipartBoundary),
_ => FormData(),
};
}