dart-lang / dart-lang/http

Request set bodyFields, need to be able to have List<String> for a map value

Open
#47 12 comments 2 reactions 0 assignees View on GitHub
contributions-welcome type-enhancement
Dominant language
Dart
Stars
1.1k
Forks
419
Avg merge
4d 14h
Merged PRs (30d)
8

Description

_From @rwrozelle on December 14, 2016 14:5_

I need to be able to set bodyFields with a map that has multiple values for a single key, so map would be `Map` where Object would be either a `String` or `List` at runtime. Here is an example map: `{q: *:*, start: 0, rows: 0, facet: true, stats: true, stats.field: [{!countDistinct=true }vin, {!countDistinct=true }veh_year]}`

Here is sample code of changes I would like, this is example code only, implementation could be a lot better:

```dart
//http 0.11.3+9/lib/src/request.dart - change Map input from to
set bodyFields(Map fields) {
var contentType = _contentType;
if (contentType == null) {
_contentType = new MediaType("application", "x-www-form-urlencoded");
} else if (contentType.mimeType != "application/x-www-form-urlencoded") {
throw new StateError('Cannot set the body fields of a Request with '
'content-type "${contentType.mimeType}".');
}

this.body = mapToQuery(fields, encoding: encoding);
}

//http 0.11.3+9/lib/src/utils.dart - rewrite to put key in multiple times when value is a list.
/// Converts a [Map] from parameter names to values to a URL query string.
///
/// mapToQuery({"foo": "bar", "baz": "bang"});
/// //=> "foo=bar&baz=bang"
String mapToQuery(Map map, {Encoding encoding}) {
String retStr = '';
String delimiter = '';
var keys = map.keys;
for (String key in keys) {
var value = map[key];
if (value is List) {
for (String subvalue in value) {
retStr +="${delimiter}${key}=${subvalue}";
delimiter='&';
}
}
else {
retStr +="${delimiter}${key}=${value}";
delimiter='&';
}
}
return retStr;
}
```

_Copied from original issue: dart-lang/sdk#28098_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.