get method

  1. @override
Future<JSON> get({
  1. required String host,
  2. String? path,
  3. Map<String, String>? queryParameters,
  4. Map<String, String>? headers,
  5. bool isHttps = true,
  6. Duration timeout = const Duration(seconds: 5),
})
override

Fetches the JSON object from the HTTP endpoint using the path, queryParameters and headers for the RESTful service host.

If the response body is not a Map<String, dynamic>, the body will be wrapped in a hashmap in a field with the key "data".

The HTTP request/response properties are added to the JSON with the following keys:

  • the "_%path" is the endpoint path string;
  • the "_%status" is the response statusCode;
  • the "_%query" is a hashmap containing the request query parameters;
  • the "_%headers" is a hashmap containing the request headers;

If an error occurs the "data" field will contain the error object.

If no response is received, the "data" field contains a generic error message and the status code is -1.

Implementation

@override
Future<JSON> get(
    {required String host,
    String? path,
    Map<String, String>? queryParameters,
    Map<String, String>? headers,
    bool isHttps = true,
    Duration timeout = const Duration(seconds: 5)}) async {
  final uri = _uri(
      host: host,
      path: path,
      queryParameters: queryParameters,
      isHttps: isHttps);
  var data = '"An unspecified error has occurred. No response was received '
      'from the server."';
  var statusCode = -1;
  try {
    final response = await _getUri(uri, headers: headers, timeout: timeout);
    data = response?.body ?? data;
    statusCode = response?.statusCode ?? statusCode;
  } catch (e) {
    data = e.toString();
  }
  return _decodeJson(data, uri, headers, statusCode);
}