Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rwrozelle opened this issue Dec 14, 2016 · 4 comments
Closed

Comments

@rwrozelle
Copy link

rwrozelle commented Dec 14, 2016

I need to be able to set bodyFields with a map that has multiple values for a single key, so map would be Map<String, Object> 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:

//http 0.11.3+9/lib/src/request.dart - change Map input from <String, String> to <String, Object>
  set bodyFields(Map<String, Object> 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<String, Object> 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;
  }
@lrhn
Copy link
Member

lrhn commented Dec 14, 2016

I think this is referring to the http package, so I've filed an issue over there pointing back to this.

@rwrozelle
Copy link
Author

Thank you,

I followed the link called out at the bottom of https://pub.dartlang.org/packages/http

http://dartbug.com/new

@lrhn
Copy link
Member

lrhn commented Dec 14, 2016

Yeah, that's probably an old link. I'll file an issue for fixing that too. :)

@vsmenon
Copy link
Member

vsmenon commented Dec 14, 2016

This issue was moved to dart-lang/http#47

@vsmenon vsmenon closed this as completed Dec 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants