http - How to avoid Uri encoding using Uri.https from Dart library
问题描述
I've this snippet to access a rest service. But the method Uri.https are undesirable encoding the url resulting in:
https://unogs-unogs-v1.p.mashape.com/aaapi.cgi?query=new%3A7-%211900%2C2018-%210%2C5-%210%2C10-%210-%21Any-%21Any-%21Any-%21gt100-%21Yes&type=ns&st=adv&orderBy=Relevance&andOr=and
I want to let Url as is: 'new:7-!1900,2018-!0,5-!0,10-!0-!Any-!Any-!Any-!gt100-!Yes'
In Retrofit/Kotlin I simply put encoded=false
to Query parameter: @Query("q", encoded = true)
import 'dart:async';
import 'package:http/http.dart' as http;
class FetchMovies {
final queryParameters = {
'query': 'new:7-!1900,2018-!0,5-!0,10-!0-!Any-!Any-!Any-!gt100-!Yes',
'type': 'ns',
'st': 'adv',
'orderBy': "Relevance",
'andOr': 'and'
};
Future<http.Response> execute() async {
var uri = Uri.https(
'unogs-unogs-v1.p.mashape.com', '/aaapi.cgi', queryParameters, );
print(uri.toString());
return await http.get(uri, headers: {
'X-Mashape-Key': '',
'Accept': 'application/json'
});
}
}
解决方案
即使添加查询部分也unencodedPath
可以解决问题,这样我就必须摆脱queryParameters
并且必须以动态方式构建查询字符串。我发现的最佳解决方案是使用decodeComponent
Uri 类的实用方法:
return await http.get(Uri.decodeComponent(uri.toString()), headers: {
'X-Mashape-Key': '',
'Accept': 'application/json'
});
推荐阅读
- javascript - 如何将对象数组映射到新数组
- typescript - 如何使用 Typescript 中的现有枚举创建超级集枚举
- android - 使用 ConstraintLayout 在 RecyclerView 中 Listview 不可滚动
- ruby - 在 Ruby 中将对象转换为哈希
- reactjs - Docker-Compose 用于反应和烧瓶使用 docker-compose up 执行容器
- mule - 您使用这些参数调用了函数“值选择器”:在 mule 4
- javascript - Observable Notebooks 中的 For 循环和日志记录计数器
- php - 验证来自 POST 值的输入?
- python - Rock Paper Scissor Spock Lizard 不能在 Python 中工作
- kubernetes - 在 Stackdriver 和 Istio 上监控 GKE