Dart中的jQuery.param

问题描述

我需要发送带有 Content-Type:application / x-form-urlencoded

的正文

json中的body请求如下

{"skip":0,"take":50,"pageSize":50,"page":0,"filter":{"logic":"and","filters":[{"operator":"startsWith","field":"nazione","value":"it"}]}}

我需要将其转换为:

skip=0&take=50&page=1&pageSize=50&filter%5Blogic%5D=and&filter%5Bfilters%5D%5B0%5D%5Boperator%5D=startsWith&filter%5Bfilters%5D%5B0%5D%5Bfield%5D=nazione&filter%5Bfilters%5D%5B0%5D%5Bvalue%5D=it

在jQuery中,只要将json传递给$ .param,我就可以得到此结果,在dart中是否存在类似的东西?

我正在使用http(来自“ package:http / http.dart”),并且我不知道如何转换正文以便在http.post中使用:

http.post(url,headers: Map<String,String>.from({'application/x-www-form-urlencoded; charset=UTF-8'}),body: ???)

主体是通过以下方式构建的:

var filters = [
    Map<String,String>.from({
        'operator': 'startsWith','field': 'nazione','value': 'it'
    })
];

var filter = Map<String,dynamic>.from({
    'logic': 'and','filters': filters
});

var body = Map<String,dynamic>.from({
    'skip': 0,'take': 50,'pageSize': 50,'page': 1,'filter': filter
});

尝试

Uri(
    scheme: 'https',host: 'myhost',port: 8443,queryParameters: body,pathSegments: ["api","coutries","grid"]);

我得到了错误

type 'int' is not a subtype of type 'Iterable<dynamic>'

因为我应该将body转换为:

Map<String,String>.from({
      'take': 50.toString(),'skip': 0.toString(),'pageSize': 50.toString(),'page': 1.toString(),'filter[logic]': 'and','filter[filters][0][operator]': 'startsWith','filter[filters][0][field]': 'nazione','filter[filters][0][value]': 'it'
})

在这种情况下,Uri中的queryParameter起作用。我是Dart的新手,所以在尝试从零开始解决此问题之前,我想知道是否存在一些简单而正确的方法来实现此目的。

解决方法

使用Uri类的queryParameters:

Uri(scheme: 'http',host: 'example.com',queryParameters: {...})

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...