I\'m trying to write some tests for my Dart server application, and I\'ve been using the HttpClient class (along with the related HttpClientRequest and HttpClientResponse cl
The HttpClientResponse
object is a Stream
, so you can just read it with the listen()
method:
response.listen((List data) {
//data as bytes
});
You can also use the codecs from dart:convert
to parse the data. The following example reads the response contents to a String:
import 'dart:io';
import 'dart:convert';
import 'dart:async';
Future readResponse(HttpClientResponse response) {
final completer = Completer();
final contents = StringBuffer();
response.transform(utf8.decoder).listen((data) {
contents.write(data);
}, onDone: () => completer.complete(contents.toString()));
return completer.future;
}