request method
Implementation
Future<WebPresenterRestResponse> request(String method, String path,
{Object? body, String? contentType, Map<String, String>? headers}) async {
final request = await _http.openUrl(
method,
baseUri.resolve(path.startsWith('/') ? path.substring(1) : path),
);
request.headers.set(HttpHeaders.acceptHeader, 'application/json');
headers?.forEach(request.headers.set);
if (body != null) {
if (body is String) {
request.headers.contentType =
ContentType.parse(contentType ?? 'application/xml; charset=utf-8');
request.write(body);
} else {
request.headers.contentType = ContentType.json;
request.write(jsonEncode(body));
}
}
final response = await request.close();
final text = await utf8.decoder.bind(response).join();
Object? value;
if (text.isNotEmpty) {
try {
value = jsonDecode(text);
} on FormatException {
value = text;
}
}
final result =
WebPresenterRestResponse(response.statusCode, value, response.headers);
if (response.statusCode < 200 || response.statusCode >= 300) {
throw WebPresenterRestException(result);
}
return result;
}