0
votes

I'm using the GeoDb API https://rapidapi.com/wirefreethought/api/geodb-cities?endpoint=5978313be4b06b85e4b0da1e to get city region and country. At the link you can test the API and get a code snippet in the language you use and making a request with city namePrefix parameter I get a valid response code 200.

When testing a location search I specified location as +44.5004+11.3346, radius 15and types as ADM2 and it correctly finds my city. So having a look at the code snippet to make my request from Flutter I see that the suggested url can either be

https://wft-geo-db.p.rapidapi.com/v1/geo/cities?location=%252B44.5004%252B11.3346&radius=15&types=ADM2 when encoding + sign with %252B as for Node.js HTTP, Javascript jQuery, Swift NSURLSession and others

or https://wft-geo-db.p.rapidapi.com/v1/geo/cities?location=%2B44.5004%2B11.3346&radius=15&types=ADM2 when encoding +sign with %2Bas for PHP HTTP v.2

I tried both versions in my request but I always get a response code 400.

As there is no Flutter code snippet and I know nothing about http requests I can't work out a valid format for the request. Can you spot what I'm doing wrong whit the request? Thank you very much for your time and help. I'm using http package and convert. The method is:

Future<List<String>> getCityDb(String isoLocationDb) async {
    print('getCityDb called');
    //location  Request failed with status: 400.
    final String request =
        'https://wft-geo-db.p.rapidapi.com/v1/geo/cities?location=%2B44.5004%2B11.3346&radius=15&languageCode=IT&types=ADM2';

//    final String request =
//        'https://wft-geo-db.p.rapidapi.com/v1/geo/cities?location=%252B44.5004%252B11.3346&radius=15&languageCode=IT&types=ADM2';

    // namePrefix works just fine
//    final String request =
//        'https://wft-geo-db.p.rapidapi.com/v1/geo/cities?namePrefix=bologna&languageCode=EN';

    var response = await get(Uri.encodeFull(request), headers: {
      'x-rapidapi-host': 'wft-geo-db.p.rapidapi.com',
      'x-rapidapi-key': apiKey,
    });

    if (response.statusCode == 200) {
//      print('response code is 200, body is: ${response.body}');

      var jsonResponse = convert.jsonDecode(response.body);
      List<dynamic> properties = jsonResponse['data'];
      print('properties are $properties');
      String name = properties.first['name'].toString();
      String region = properties.first['region'].toString();
      String country = properties.first['country'].toString();

//      print(' getCityDb ${jsonResponse.runtimeType} : $jsonResponse');
      print('getCityDb jsonResponse name is :$name');
      print('getCityDb jsonResponse region is :$region');
      print('getCityDb jsonResponse country is: $country');

      List<String> cityDb = new List<String>();
      cityDb.add(name);
      cityDb.add(region);
      cityDb.add(country);
      return cityDb;
    } else {
      print('getCityDB() Request failed with status: ${response.statusCode}.');
    }
  }
1

1 Answers

0
votes

I found the problem boing encoding the request. Changed get(Uri.encodeFull(request), headers:to get(request, headers: and is working as expected with the request https://wft-geo-db.p.rapidapi.com/v1/geo/cities?location=%2B44.5004%2B11.3346&radius=15&types=ADM2. Also the namePrefix request still works.