http url 参数无效字符错误

http url parameter Invalid Character Error

我使用 feathers.js (https://feathersjs.com/) 开发了 REST API。
尝试使用 package:http/http.dart 在 Flutter 中执行 HTTP 'read' 请求时遇到错误。 http.dart 库无法正确解析我传递给 URI 的查询参数。

我通过 Android Studio 调试控制台收到的错误是 ;

FormatException:无效字符(在字符 84)E/flutter (11338): ...lk.com/weatherobs?last=true&location[$in]=Bellambi&location[$in]=Nowra ....

错误表明方括号和 $ 符号 ('[$in]') 可能是问题所在。

_getDemoRequest() {
  String url = r"http://demoapi.ap-southeast-2.elasticbeanstalk.com/weatherobs?last=true&location[$in]=Bellambi&location[$in]=Nowra&location[$in]=Sydney Airport&location[$in]=Thredbo Top Station&location[$in]=Hobart&location[$in]=Coolangatta";
  http.read(url).then(print);    
}

在 URL 中,我尝试为原始字符串添加和不添加 'r' 作为前缀,但都无济于事。

我也尝试过将 httpClient 与参数一起使用但没有成功,并且在方括号中出现完全相同的错误,例如 '[$in]'

  String httpbaseUri = "http://xxxx.ap-southeast-2.elasticbeanstalk.com";
  String qParams = r"?last=true&location[$in]=Bellambi&location[$in]=Nowra";
  String path = "/weatherobs";
  var _uri = new Uri.http(baseUri, path, qParams);
  await httpClient.read(_uri, headers: {"Accept": "application/json"});

作为一个拥有大约 3 周 Flutter/Dart 经验的人,我认为这是一个基本问题,但经过几个小时的研究仍未找到解决方案。

URI 查询参数的结构方式(使用方括号,即 [$in])由 feathers.js 框架规定。

如有任何帮助,我们将不胜感激。

它已在另一个线程中引起我的注意 :

URL 规范 RFC 3986 通常不允许在 URL 中使用方括号。

我的问题是在 Postman、Chrome 浏览器以及 javascript 使用 axios.js 的应用程序中按预期工作的,但在 [=47 中开发的应用程序中却没有=] 使用标准 http.read 方法。

URL 似乎不支持 [](IPv6 的主机 IP 除外)。参见 Are square brackets permitted in URLs?

请检查 API 在编码时是否接受它们:

void main() {
    var url = r'http://demoapi.ap-southeast-2.elasticbeanstalk.com/weatherobs';
    var locationKey = Uri.encodeQueryComponent(r'location[$in]');
    var qParams = 'last=true&$locationKey=Bellambi&$locationKey=Nowra&$locationKey=Sydney Airport&$locationKey=Thredbo Top Station&$locationKey=Hobart&$locationKey=Coolangatta'; 

    try {
      print(Uri.parse(url).replace(query: qParams));
    } catch(e) {
      print(e);
    }
}

DartPad example

另见 api.dartlang.org/stable/1.24.3/dart-core/Uri/…

您可以使用这个 flutter 包,它允许您从 flutter 应用程序与您的 feathers js 服务器通信,如中所述: