post method

  1. @override
Future<JSON?> post({
  1. required String host,
  2. String? path,
  3. Map<String, String>? queryParameters,
  4. Map<String, String>? headers,
  5. JSON? body,
  6. bool isHttps = true,
})
override

Posts JSON body to the HTTP endpoint using thehost, path, queryParameters and headers using HTTPS if isHttps.

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?> post(
    {required String host,
    String? path,
    Map<String, String>? queryParameters,
    Map<String, String>? headers,
    JSON? body,
    bool isHttps = true}) async {
  final uri = _uri(
      host: host,
      path: path,
      queryParameters: queryParameters,
      isHttps: isHttps);
  final response =
      await http.post(uri, headers: headers, body: jsonEncode(body));

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response, convert to JSON and
    // return the result.
    return _decodeJson(response.body, uri, headers, response.statusCode);
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('The server failed to respond in a timely manner');
  }
}