doRequest static method
This is a static method that performs an HTTP request. It takes three parameters:
- method: An enum value of type HttpMethods (presumably defined elsewhere) that represents the HTTP method to be used for the request (e.g., GET, POST, etc.).
- url: A string representing the URL to which the request will be sent.
- body: A map of type Map<String, dynamic> representing the request body. Within the method:
- A headers map is defined, specifying the content type as 'application/x-www-form-urlencoded'.
- An http.Request object is created using the specified method and url.
- The body map is converted to a string key-value pair and assigned to request.bodyFields.
- The headers are added to the request using request.headers.addAll(headers).
- The request is sent using await request.send(), and the response is stored in a http.StreamedResponse object named response.
- The status code of the response is checked. If it is 200 (indicating success), the response body is read and returned as a string using await response.stream.bytesToString(). If the status code is not 200, an empty string is returned.
Note that this code relies on the http package, which may need to be imported separately for the class to work correctly.
Implementation
///* A headers map is defined, specifying the content type as 'application/x-www-form-urlencoded'.
///* An http.Request object is created using the specified method and url.
///* The body map is converted to a string key-value pair and assigned to request.bodyFields.
///* The headers are added to the request using request.headers.addAll(headers).
///* The request is sent using await request.send(), and the response is stored in a http.StreamedResponse object named response.
///* The status code of the response is checked. If it is 200 (indicating success), the response body is read and returned as a string using await response.stream.bytesToString().
/// If the status code is not 200, an empty string is returned.
///
///Note that this code relies on the http package, which may need to be imported separately for the class to work correctly.
static Future<String> doRequest(HttpMethods method, String url, Map<String, dynamic> body) async {
// Defines the headers for the HTTP request
final Map<String, String> headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
// 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
Map<String, String> bodyString = body.map((key, value) => MapEntry(key, value.toString()));
request.bodyFields = bodyString;
// 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 "";
}
}