parseData method
Parses and returns request data from GET, POST, and file fields.
The result is stored in _dataRequest
.
Implementation
Future<Map> parseData() async {
Map<String, dynamic> post = {};
Map<String, dynamic> get = {};
Map<String, dynamic> file = {};
String content = "";
// For GET
var params = _rq.uri.queryParameters;
get.addAll(params);
// For POST or PUT
if (method == RequestMethods.POST || method == RequestMethods.PUT) {
// Form Forms
if (headers.contentType
.toString()
.toLowerCase()
.contains("application/x-www-form-urlencoded")) {
try {
content = await utf8.decoder.bind(stream).join();
var body = QueryString.parse(content);
post.addAll(body);
} catch (e) {
Console.e(e);
}
} else if (headers.contentType
.toString()
.toLowerCase()
.contains("multipart/form-data")) {
var data = await getHeaderFormData();
post.addAll(data['fields']);
file.addAll(data['files']);
} else if (headers.contentType
.toString()
.toLowerCase()
.contains("application/json")) {
try {
content = await utf8.decoder.bind(stream).join();
var data = jsonDecode(content);
post.addAll(data);
} catch (e) {
Console.e(e);
}
} else {
try {
var bodyBytes = await stream
.fold<List<int>>([], (bytes, chunk) => bytes..addAll(chunk));
content = String.fromCharCodes(bodyBytes);
} catch (e) {
Console.e(e);
}
}
}
_dataRequest = {
'POST': post,
'GET': get,
'FILE': file,
'CONTENT': content,
};
return _dataRequest;
}