代码之家  ›  专栏  ›  技术社区  ›  kite_n_code

http url参数无效字符错误

  •  4
  • kite_n_code  · 技术社区  · 7 年前

    我已经使用Feather开发了一个RESTAPI。js公司( https://feathersjs.com/ ).
    尝试使用包HTTP/HTTP在Flutter中执行HTTP“读取”请求时。dart我遇到了一个错误。http。dart库无法正确解析我传递给URI的查询参数。

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

    FormatException:无效字符(位于字符84处)E/flatter (11338): ...lk。通信/天气预报?最后一个=真(&L);位置[$英寸]=Bellambi&位置[$英寸]=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周颤振/飞镖经验的人,我认为这是一个基本问题,但几个小时的研究没有发现解决方案。

    URI查询参数的构造方式(使用方括号ie[$in])由特征决定。js框架。

    任何帮助都将不胜感激。

    我在另一个帖子中注意到了这一点 https://stackoverflow.com/questions/40568/are-square-brackets-permitted-in-urls :

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

    当get请求在Postman、Chrome浏览器以及使用 axios.js ,但不是在使用标准http在颤振/Dart中开发的应用程序中。读取方法。

    2 回复  |  直到 7 年前
        1
  •  4
  •   Günter Zöchbauer    7 年前

    看起来不像 [] 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。达特朗。org/stable/1.24.3/dart-core/Uri/

        2
  •  0
  •   Dahkenangnon    4 年前

    您可以使用此颤振软件包,该软件包允许您从颤振应用程序与feathers js服务器进行通信,如中所述: https://stackoverflow.com/a/65538226/12461921