postMethodWithToken method

Future<(int, Map<String, dynamic>)> postMethodWithToken({
  1. String? url,
  2. dynamic token,
  3. dynamic body,
})

Sends an HTTP POST request to the specified url with an authentication token, request body, and optional API key.

Returns a Future that completes with a (int, Map<String, dynamic>) containing the status code and the response data.

Throws an exception if the request fails.

Example usage:

var response = await ApiMethods().postMethodWithToken(url: "https://api.example.com/data", token: "your_token_here", body: jsonEncode({"key": "value"}));

Implementation

Future<(int statusCode, Map<String, dynamic> data)> postMethodWithToken({
  String? url,
  var token,
  var body,
}) async {
  try {
    var response = await http.post(
      Uri.parse(url ?? ""),
      headers: (xApiKey ?? "").isNotEmpty
          ? {
              "Content-Type": "application/json",
              "Authorization": "Bearer $token",
              'x-api-key': (xApiKey ?? ""),
            }
          : {
              "Content-Type": "application/json",
              "Authorization": "Bearer $token",
            },
      body: body,
    );
    return _handleResponse(response);
  } catch (error, stackTrace) {
    throw (error, stackTrace);
  }
}