Running Multiple Commands in Sequence

 

Running http commands in sequence

Use http.Client instead of http.post to keep connection open.

The documentation for the Dart http package has the following advice:

If you’re making multiple requests to the same server, you can keep open a persistent connection by using a Client rather than making one-off requests. If you do this, make sure to close the client when you’re done:

var url = 'http://192.168.1.1/osc/commands/execute';
Map<String, String> headers = {
  "Content-Type": "application/json;charset=utf-8"
};
try {
  for (var i = 0; i < numberOfImages; i++) {
    print('deleting file ${urlList[i]}');

    var body = jsonEncode({
      'name': 'camera.delete',
      'parameters': {
        'fileUrls': [urlList[i]]
      }
    });
    var testResponse = await client.post(url, headers: headers, body: body);
    if (testResponse.statusCode == 200) {
      print('successfully deleted');
    } else {
      print(
          'Something went wrong.  Check http status code: ${testResponse.statusCode}');
    }
  }
} catch (e) {
  print(e);
} finally {
  client.close();
  print('closed client');
}