Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving the response body from an HttpClientResponse

Tags:

dart

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 classes to make test requests to the server (note that I'm using these classes because I need the dart:io package for running the server, so I can't also import dart:html). This has been going fairly well so far, and I've been able to write tests to check that the server is returning responses with the correct HTTP Status code. The base of the code I've been using to make these test calls is as follows:

Future<HttpClientResponse> makeServerRequest(String method, Uri uri, [String jsonData]) async {
  HttpClient client = new HttpClient();
  HttpClientRequest request = await client.openUrl(method, uri);
  request.write(jsonData);
  return request.close();
}

Now I need to write a test that makes sure that the body of the response, not just the status code, is correct. The problem is that I can't seem to find anything that allows me to actually access the response body in the HttpClient* classes. The closest I've been able to find so far is the HttpClientResponse.contentLength property, but that only tells me how big the response body is, and isn't the actual content.

How do I retrieve the response body from these requests? Or, if you aren't able to, is there some other way I can make the requests on a server side application so I can read the responses?

like image 574
Michael Fenwick Avatar asked Jan 06 '15 23:01

Michael Fenwick


People also ask

How do I get http body response?

To get the response body as a string we can use the EntityUtils. toString() method. This method read the content of an HttpEntity object content and return it as a string. The content will be converted using the character set from the entity object.


2 Answers

The HttpClientResponse object is a Stream, so you can just read it with the listen() method:

response.listen((List<int> 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<String> readResponse(HttpClientResponse response) {
  final completer = Completer<String>();
  final contents = StringBuffer();
  response.transform(utf8.decoder).listen((data) {
    contents.write(data);
  }, onDone: () => completer.complete(contents.toString()));
  return completer.future;
}
like image 105
luizmineo Avatar answered Nov 05 '22 21:11

luizmineo


Low level

Here is the await for version of collecting the response stream. It's a little more compact than using a completer.

Future<String> readResponse(HttpClientResponse response) async {
  final contents = StringBuffer();
  await for (var data in response.transform(utf8.decoder)) {
    contents.write(data);
  }
  return contents.toString();
}

You should wrap it in a try catch block to handle errors.

High level

Most of the time from the client side you would use the http library instead:

// import 'package:http/http.dart';

Response response = await get(url);
String content = response.body;

See this article for more details.

like image 38
Suragch Avatar answered Nov 05 '22 20:11

Suragch