0

I've similar question like Urlencoding in Dart. I can encode Map, by HttpRequest.postFormData. But JQuery post method can encode Map<String, dynamic>. JQuery example:

$.post("controller", 
{actualTime: 1357089552, events: [{priceInsert: 1.32128, priceExecution: 1.32128}]},
function(data) {/*handle*/});

Firebug HttpRequest post view:

actualTime                  1357089552
events[0][priceExecution]   1.32128
events[0][priceInsert]      1.32128

Payload source is:

actualTime=1357089552&events%5B0%5D%5BpriceInsert%5D=1.32128&events%5B0%5D%5BpriceExecution%5D=1.32128

Dart can't do it easily. Someone has this problem solved?

PHP with nette requires to set some header:

X-Requested-With:XMLHttpRequest
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Community
  • 1
  • 1
Zdeněk Mlčoch
  • 722
  • 6
  • 17

1 Answers1

0

I've some dirty quick fix. Not complete, not tested, working for me:

Map<String, dynamic> data = {"actualTime": 1357089552, "events": [{"priceInsert": 1.32128, "priceExecution": 1.32128}]};
StringBuffer urlData = new StringBuffer("");
bool first = true;
void urlEncode(dynamic sub, String path){
    if(sub is List){
      for(int i = 0;i<sub.length;i++){
        urlEncode(sub[i], "$path%5B$i%5D");
      }
    }else if(sub is Map){
      sub.forEach((k,v){
        if(path == ""){
          urlEncode(v, "${Uri.encodeQueryComponent(k)}");
        }else{
          urlEncode(v, "$path%5B${Uri.encodeQueryComponent(k)}%5D");
        }
      });
    }else{
      if(!first){
        urlData.write("&");
      }
      first = false;
      urlData.write("$path=${Uri.encodeQueryComponent(sub.toString())}");
    }
}
urlEncode(data, "");

HttpRequest xhr = new HttpRequest();
  xhr
    ..open('POST', url)
    ..onLoadEnd.listen((ProgressEvent event){/*handle success*/}, onError: (){/*handle error*/})
    ..setRequestHeader("X-Requested-With", "XMLHttpRequest")
    ..setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
    ..send(urlData.toString());
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Zdeněk Mlčoch
  • 722
  • 6
  • 17