3

I'm playing a bit with Flutter and try to perform a http get request. Though I'm always getting an empty body in the response.

For example with the following code :

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

[...]

http.Client client = new http.Client();
client
    .get("https://www.googleapis.com/books/v1/volumes?q=$text")
    .then((http.Response response) {
  print(response.statusCode);
  print(response.body);
  setState(() {
    _isLoading = false;
  });
});

I get the following result :

200
{

Do you have any ideas ?

Thanks by advance !

EDIT

It appears that the problem only happens on iOS devices. It works as expected on Android.

Jonathan
  • 352
  • 2
  • 9
  • 1
    Did you try to breakpoint and check the body? I think I remember some issue with multi-line prints on Windows. – Rene Mar 14 '18 at 12:20
  • Not directly related to your question but https://stackoverflow.com/questions/48477625/how-to-use-google-api-in-flutter/48485898#48485898 might be interesting to you if you need authentication with Google API on Flutter. – Günter Zöchbauer Mar 15 '18 at 06:39
  • Note that you're not including any headers. Try adding `Accept`: `application.json`. – Tobe Osakwe Mar 20 '18 at 20:35
  • Maybe Dio will simplify things: https://pub.dartlang.org/packages/dio#-example-tab- – SteAp Jul 27 '18 at 22:34

3 Answers3

1

Can you try this below code. The code is untested.

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

main() async {
  try {
    var client = new HttpClient();
    String text = "example";

    var uri = Uri.parse("https://www.googleapis.com/books/v1/volumes?q=$text");

    var request = await client.getUrl(uri);
    var response = await request.close();
    var responseBody = await response.transform(UTF8.decoder).join();
    print(responseBody);
  } catch (exception) {
    print(exception);
  }
}
Purus
  • 5,701
  • 9
  • 50
  • 89
1

Probably you forget the headers , for example :

headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'X-Requested-With': 'XMLHttpRequest',
    },
Álvaro Agüero
  • 4,494
  • 1
  • 42
  • 39
0

Try debugPrint() instead of print(). It will print all body to console

https://flutter.io/debugging/#print-and-debugprint-with-flutter-logs