urlencoding in Dart

后端 未结 5 2089
鱼传尺愫
鱼传尺愫 2020-12-01 13:31

Is there a function to do urlencoding in Dart? I am doing a AJAX call using XMLHttpRequest object and I need the url to be url encoded.

I did a search on dartlang.or

相关标签:
5条回答
  • 2020-12-01 14:14
    Uri.encodeComponent(url); // To encode url
    Uri.decodeComponent(encodedUrl); // To decode url
    
    0 讨论(0)
  • 2020-12-01 14:17

    I wrote this small function to convert a Map into a URL encoded string, which may be what you're looking for.

    String encodeMap(Map data) {
      return data.keys.map((key) => "${Uri.encodeComponent(key)}=${Uri.encodeComponent(data[key])}").join("&");
    }
    
    0 讨论(0)
  • 2020-12-01 14:28
    var uri = 'http://example.org/api?foo=some message';
    var encoded = Uri.encodeFull(uri);
    assert(encoded == 'http://example.org/api?foo=some%20message');
    
    var decoded = Uri.decodeFull(encoded);
    assert(uri == decoded);
    

    http://www.dartlang.org/docs/dart-up-and-running/contents/ch03.html#ch03-uri

    0 讨论(0)
  • 2020-12-01 14:31

    Update: There is now support for encode/decode URI in the Dart Uri class

    Dart's URI code is placed in a separate library called dart:uri (so it can be shared between both dart:html and dart:io). It looks like it currently does not include a urlencode function so your best alternative, for now, is probably to use this Dart implementation of JavaScript's encodeUriComponent.

    0 讨论(0)
  • 2020-12-01 14:32

    I dont' think there is yet. Check out http://unpythonic.blogspot.com/2011/11/oauth20-and-jsonp-with-dartin-web.html and the encodeComponent method.

    Note, it's lacking some characters too, it needs to be expanded. Dart really should have this built in and easy to get to. It may have it in fact, but I didn't find it.

    0 讨论(0)
提交回复
热议问题