我正在使用GeoDb API https://rapidapi.com/wirefreethought/api/geodb-cities?endpoint=5978313be4b06b85e4b0da1e来获取城市地区和国家。 在该链接上,您可以测试API并使用您使用的语言获取代码段,并使用city namePrefix参数进行请求,我将获得有效的响应代码200。
在测试位置搜索时,我将位置指定为+44.5004+11.3346
,将半径指定为15
,并键入ADM2
,它会正确找到我的城市。
因此,看看从Flutter提出请求的代码片段,我发现建议的网址可以是
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,与PHP HTTP v.2一样用+
编码%2B
符号时
我在请求中尝试了两种版本,但始终得到响应代码400。
由于没有Flutter代码段,而且我对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}.');
}
}
答案 0 :(得分:0)
我发现问题无法对请求进行编码。
将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请求仍然有效。