I have the following rest API written with flask (python)
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route('/load/<path>', methods=['GET', 'POST'])
def get_gene_list(path):
gene_list_dict = {}
print(path)
try:
with open(path, 'r') as f:
for line in f:
name, list = line.split('\t')
gene_list_dict[name] = list
except FileNotFoundError:
return jsonify({'sent': path, 'error': 'file not found'})
gene_list_dict['error'] = None
return jsonify(gene_list_dict)
if __name__ == '__main__':
app.run(debug=True)
And the following code on dart sends the GET request:
persistentFooterButtons: [
RaisedButton(
onPressed: () {
loadGeneList(
'SOME\\HARD\\CODED\\ABSOLUTE\\PATH');
},
child: Text('Load CSV'),
)
The function loadGeneList is implemented as follows:
Future<void> loadGeneList(query) async {
String url = 'http://10.0.2.2:5000/load/$query';
http.Response response = await http.get(url, headers: {
"Content-Type": "application/json",
});
print(response.body);
}
When I trigger the request, I get on the dart console the following traceback:
E/flutter (30778): [ERROR:flutter/lib/ui/ui_dart_state.cc(171)] Unhandled Exception: Connection closed while receiving data
E/flutter (30778): #0 IOClient.send.<anonymous closure>
package:http/src/io_client.dart:50
E/flutter (30778): #1 _invokeErrorHandler (dart:async/async_error.dart:16:24)
E/flutter (30778): #2 _HandleErrorStream._handleError (dart:async/stream_pipe.dart:282:9)
E/flutter (30778): #3 _ForwardingStreamSubscription._handleError (dart:async/stream_pipe.dart:161:13)
E/flutter (30778): #4 _HttpClientResponse.listen.<anonymous closure> (dart:_http/http_impl.dart:438:16)
E/flutter (30778): #5 _rootRunBinary (dart:async/zone.dart:1214:47)
E/flutter (30778): #6 _CustomZone.runBinary (dart:async/zone.dart:1107:19)
E/flutter (30778): #7 _CustomZone.runBinaryGuarded (dart:async/zone.dart:1013:7)
E/flutter (30778): #8 _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:376:15)
E/flutter (30778): #9 _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:394:16)
E/flutter (30778): #10 _BufferingStreamSubscription._addError (dart:async/stream_impl.dart:294:7)
E/flutter (30778): #11 _ForwardingStreamSubscription._addError (dart:async/stream_pipe.dart:132:11)
E/flutter (30778): #12 _addErrorWithReplacement (dart:async/stream_pipe.dart:180:8)
E/flutter (30778): #13 _HandleErrorStream._handleError (dart:async/stream_pipe.dart:287:11)
E/flutter (30778): #14 _ForwardingStreamSubscription._handleError (dart:async/stream_pipe.dart:161:13)
E/flutter (30778): #15 _rootRunBinary (dart:async/zone.dart:1214:47)
E/flutter (30778): #16 _CustomZone.runBinary (dart:async/zone.dart:1107:19)
E/flutter (30778): #17 _CustomZone.runBinaryGuarded (dart:async/zone.dart:1013:7)
E/flutter (30778): #18 _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:376:15)
E/flutter (30778): #19 _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:394:16)
E/flutter (30778): #20 _BufferingStreamSubscription._addError (dart:async/stream_impl.dart:294:7)
E/flutter (30778): #21 _SyncStreamControllerDispatch._sendError (dart:async/stream_controller.dart:812:19)
E/flutter (30778): #22 _StreamController._addError (dart:async/stream_controller.dart:690:7)
E/flutter (30778): #23 _StreamController.addError (dart:async/stream_controller.dart:642:5)
E/flutter (30778): #24 _HttpParser._reportBodyError (dart:_http/http_parser.dart:1155:22)
E/flutter (30778): #25 _HttpParser._onDone (dart:_http/http_parser.dart:864:9)
The file is being parsed correctly and the dictionary is generated correctly with python.
EDIT :
Solved this issue by adding the following to the python script:
from werkzeug.serving import WSGIRequestHandler
And before the app.run(...) :
WSGIRequestHandler.protocol_version = "HTTP/1.1"