post method

Future<Response> post(
  1. String url, {
  2. Map? data,
  3. String tokenName = 'Authorization',
  4. List? dataAsList,
  5. Map<String, String>? headers,
})

Effettua una post

Solo uno fra data e dataAsList può essere specificato

Implementation

Future<Response> post(
  String url, {
  Map? data,
  String tokenName = 'Authorization',
  List? dataAsList,
  Map<String, String>? headers,
}) async {
  assert(
    (dataAsList != null && data == null) ||
        (dataAsList == null && data != null) ||
        (dataAsList == null && data == null),
    "Solo uno fra 'dataAsList' e 'data' può essere specificato",
  );

  (headers ??= <String, String>{})
      .putIfAbsent('Content-Type', () => 'application/json');

  try {
    final uri = Uri.parse(url);

    late String body;

    if (dataAsList != null) {
      body = jsonEncode(dataAsList);
    } else if (data != null) {
      body = jsonEncode(data);
    } else {
      body = jsonEncode({});
    }

    final response = await _client.post(
      uri,
      body: body,
      headers: headers,
    );

    _printLog(url, response, 'POST');
    return response;
  } catch (e) {
    logger.e(
      // ignore: lines_longer_than_80_chars
      '[Provider-POST] Error on url $url: ${e.toString().replaceAll("\n", ' ')}',
    );
    return Response(jsonEncode({'code': 'GENERIC_ERROR'}), 500);
  }
}