我正在尝试将JSON请求参数放入http.get请求中。我甚至尝试遵循这个Flutter:为Http GET请求发送JSON正文,但是没有成功。无论我将什么放入params变量中,我都会从后端得到所有结果。我已经用postman测试了后端,一切都正常。
这是我的Flutter代码
Future<List<Country>> fetchCountries(String name) async {
final token = Provider.of<Auth>(context, listen: false).token;
final params = {"name": "Uk"};
try {
Uri uri = Uri.parse(APIPath.findCountry());
final newUri = uri.replace(queryParameters: params);
print(newUri); //prints http://localhost:8080/country/find?name=uk
final response = await http.get(newUri,
headers: [APIHeader.authorization(token), APIHeader.json()]
.reduce(mergeMaps));
final jsonResponse = json.decode(response.body);
if (response.statusCode == 200) {
Iterable list = jsonResponse['result'];
print(list);
return list.map((model) => Country.fromJson(model)).toList();
} else {
throw HttpException(jsonResponse["error"]);
}
} catch (error) {
throw error;
}
}
将body放入http.get请求中无法像http.post请求那样工作。有任何想法我做错了什么吗?
APIHeader.json()
中发送了"Content-Type": "application/json"
,但仍无法将正文添加到我的GET请求中,所以我想我会按照链接中的问题进行操作,但那也没有起作用...好吧,那我就改为使用POST请求。我不想在我的URL中使用参数,因为有很多参数...虽然对于这个查询不是很多,但在其他查询中有很多,所以我想将它们全部放在正文中。 - delmin