0

Read HttpClientResponse into a String

Function Dart 1 revisions 135 5 months ago 5 months ago
Future<String> readResponse(HttpClientResponse response) {
  var completer = new Completer();
  var contents = new StringBuffer();
  response.transform(UTF8.decoder).listen((String data) {
    contents.write(data);
  }, onDone: () => completer.complete(contents.toString()));
  return completer.future;
}

Simply pass it the HttpClientResponse you get from request.close() and it'd return a Future that eventually resolves to a String. (maybe use await here)

This requires a few obvious stuff:

import 'dart:async';
import 'dart:io';
import 'dart:convert';