Search code examples
flutterdarthttp

I'm not receiving response from API when making a post requisition in FLUTTER


I'm facing a strange error when I'm sending data to an API.

I have some endpoints that work pretty fine when I send and receive data. But one of those is giving me some trouble.

I'll try to explain.

So when I send a data object through the POST method using the HTTP package, the data which was sent is reaching the database and is added, but nothing is coming back from requisition.

Future postCupomFiscal(CupomFiscal cupom) async {
    log(jsonEncode(cupom.toJson()));

    var config = await LojaConfigs.iniciarConfigs();
    var url = Uri.parse('http://127.0.0.1:8082/api/v1/carga_venda');


    var client = http.Client();

    try {
      var response = await client
          .post(url,
              headers: {
                'Content-Type': 'application/json; charset=UTF-8',
                'ambiente': '${config['ambiente']}',
                'cnpj_loja': '${config['cnpj_loja']}',
                'chave_acesso': '${config['chaveacesso']}',
                'token': '${config['token']}'
              },
              body: jsonEncode(cupom.toJson()))
          .timeout(const Duration(seconds: 15));

      if (response.statusCode == 200 || response.statusCode == 201) {
        return jsonDecode(response.body);
      }
    } on SocketException {
      throw FetchDataException('Sem conexão com a Internet.');
    } on TimeoutException {
      throw FetchDataException('Tempo excedido.');
    } finally {
      client.close();
    }
    }
  }

It is expected that the response returns a JSON informing whether the data was inserted or not in the database.


Solution

  • I'm really happy with the time you spent with me to find a solution to my problem. I've spoken iwth backend developer from my team and we found the problem.

    When the data comes into the API the process has an UPDATE set, this process is taking more time than we expected. So while the update is not finished the API keeps freezed waiting that update conclusions and response comes back.

    Sometimes this update is taking about 10s and some more than 2min.

    We are looking for a solution to this UPDATE problem.

    I am very grateful for your time.