doJsonRequest static method
Implementation
static Future<String> doJsonRequest(HttpMethods method, String url, String body) async {
// Defines the headers for the HTTP request
final Map<String, String> headers = {
'Content-Type': 'application/json'
};
// Creates an HTTP request object with the specified method and URL
var request = http.Request(method.name, Uri.parse(url));
// Converts the body map to a string key-value pair and assigns it to request.bodyFields
request.body = body;
// Adds the headers to the request
request.headers.addAll(headers);
// Sends the request and waits for the response
http.StreamedResponse response = await request.send();
// Checks the response status code
if (response.statusCode == 200) {
// If the status code is 200 (OK), reads the response body and returns it as a string
return await response.stream.bytesToString();
}
else {
// If the status code is not 200, returns an empty string
return "";
}
}