request function
dynamic
request({})
The url
is the endpoint the function is going to attack
with the specified method
.
When method
is PUT or POST a body
is required, which is always a Map<String, dynamic>.
Working with JSON and RESTFUL Services may need to set the request headers
.
For example, 'Content-Type': 'application/json' (provided by this package, don't need to set up) or an auth header will be required.
This function returns
a List
Implementation
dynamic request({
required String url,
String method = 'GET',
Map<String, String>? headers,
Map<String, dynamic>? body,
}) async {
method = method.toUpperCase();
_throwError(method, body);
headers = headers ?? {};
if (method == 'PUT' || method == 'POST') {
headers['Content-Type'] = 'application/json';
}
final http.Response response = method == 'GET'
//GET
? await _availableFunctions[method](Uri.parse(url), headers: headers)
//PUT, POST, DELETE
: await _availableFunctions[method](Uri.parse(url),
headers: headers, body: jsonEncode(body));
// if (method == 'DELETE') print(response.body);
return response.body.isEmpty
? 'No data sent back to user'
: jsonDecode(response.body);
}