Home > Mobile >  flutter http get request with parameters
flutter http get request with parameters

Time:08-10

I want to send a GET http request with parameters, my problem is that when I add the parameters in the request URL manually it works fine, but when I pass them as parameters it returns an exception without any explanation and somehow the execution stops after Uri.https

here is the code that I want to achieve

  Future<List<LawFirm>> getLawFirms () async {
    Map<String, dynamic> parameters = {
      'total': true
    };
    final uri =
    Uri.http('www.vision.thefuturevision.com:5000',
        '/api/law-firm', parameters);
    final response = await http.get(uri);
    var dynamicResponse = jsonDecode(response.body);
    totaLawFirms = await dynamicResponse['total'];
    var lawFirms = await dynamicResponse['data'];
    List<LawFirm> list = List<LawFirm>.from(lawFirms.map((x) => LawFirm.fromJson(x)));
    print(list);
    notifyListeners();
    return list;
  }

and here is the manual way which shouldn't be applied

    final response = await get(Uri.parse('$baseURL/law-firm?total=true'));

I have also tried the dio library from pub.dev but also wasn't helpful.

And finally thanks in advance to everyone

CodePudding user response:

You may try this

Map<String, dynamic> parameters = {
  'total': true
};

var uri = Uri(
  scheme: 'http',
  host: 'www.vision.thefuturevision.com:5000',
  path: '/law-firm',
  queryParameters: parameters,
);

final response = await http.get(uri);

CodePudding user response:

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

 final response =
          await http.get(Uri.parse("${Constants.baseUrl}endpoint/param1/param2"));

Just modify your GET request like this.

CodePudding user response:

Try this

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


callAPI() async {
  String login = "sunrule";
  String pwd = "api";
  Uri url = Uri.parse(
      "http://vijayhomeservices.in/app/api/index.php?apicall=login&login=$login&password=$pwd");
  final response = await http.get(url);
  if (response.statusCode == 200) {
    final body = json.decode(response.body);
    print(body.toString());
  } else {
    throw Exception("Server Error !");
  }
}
  • Related