【发布时间】:2020-10-04 04:23:30
【问题描述】:
我正在尝试将 JSON 正文查询参数放入 http.get 请求中。我什至试图关注这个Flutter: Send JSON body for Http GET request,但没有运气。无论我将什么放入 params 变量中,我都会从后端获得所有结果。我用邮递员测试了后端,一切正常
这是我在颤振中的代码
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;
}
}
将正文放入 http.get 请求不像 http.post 请求那样工作。知道我做错了什么吗?
【问题讨论】: