Flutter: HTTP get request body is empty

7,112

Solution 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);
  }
}

Solution 2

Probably you forget the headers , for example :

headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'X-Requested-With': 'XMLHttpRequest',
    },
Share:
7,112
Jonathan
Author by

Jonathan

Updated on December 04, 2022

Comments

  • Jonathan
    Jonathan over 1 year

    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.