【问题标题】:Response code 400 from GeoDB API request Flutter来自 GeoDB API 请求 Flutter 的响应代码 400
【发布时间】:2020-03-17 19:37:22
【问题描述】:

我正在使用 GeoDb API https://rapidapi.com/wirefreethought/api/geodb-cities?endpoint=5978313be4b06b85e4b0da1e 获取城市区域和国家/地区。 在链接中,您可以测试 API 并以您使用的语言获取代码 sn-p,并使用城市 namePrefix 参数发出请求,我得到有效的响应代码 200。

在测试位置搜索时,我将位置指定为 +44.5004+11.3346,半径 15 并键入 ADM2,它正确地找到了我的城市。 因此,查看代码 sn-p 以从 Flutter 发出我的请求,我发现建议的 url 可以是

https://wft-geo-db.p.rapidapi.com/v1/geo/cities?location=%252B44.5004%252B11.3346&radius=15&types=ADM2 在编码+ 时使用%252B 签名,用于Node.js HTTP、Javascript jQuery、Swift NSURLSession 等

或https://wft-geo-db.p.rapidapi.com/v1/geo/cities?location=%2B44.5004%2B11.3346&radius=15&types=ADM2,当用%2B编码+sign时,对于PHP HTTP v.2

我在请求中尝试了这两个版本,但总是得到响应代码 400。

由于没有 Flutter 代码 sn-p 并且我对 http 请求一无所知,因此我无法为请求计算出有效的格式。 你能发现我在请求中做错了什么吗? 非常感谢您的时间和帮助。 我正在使用 http 包和转换。方法是:

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}.');
    }
  }

【问题讨论】:

    标签: http flutter


    【解决方案1】:

    我发现了对请求进行编码的问题。 将 get(Uri.encodeFull(request), headers: 更改为 get(request, headers: 并按预期处理请求 https://wft-geo-db.p.rapidapi.com/v1/geo/cities?location=%2B44.5004%2B11.3346&radius=15&types=ADM2。 namePrefix 请求也仍然有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-03
      • 1970-01-01
      • 1970-01-01
      • 2023-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      相关资源
      最近更新 更多