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 #47

Open
vsmenon opened this issue Dec 14, 2016 · 12 comments
Labels
contributions-welcome Contributions welcome to help resolve this (the resolution is expected to be clear from the issue) type-enhancement A request for a change that isn't a bug

Comments

@vsmenon
Copy link
Member

vsmenon commented Dec 14, 2016

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<String, Object> where Object would be either a String or List<String> 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;
  }

Copied from original issue: dart-lang/sdk#28098

@vsmenon
Copy link
Member Author

vsmenon commented Dec 14, 2016

From @lrhn on December 14, 2016 14:18

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

@vsmenon
Copy link
Member Author

vsmenon commented Dec 14, 2016

From @rwrozelle on December 14, 2016 14:21

Thank you,

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

http://dartbug.com/new

@vsmenon
Copy link
Member Author

vsmenon commented Dec 14, 2016

From @lrhn on December 14, 2016 14:50

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

@nex3
Copy link
Member

nex3 commented Dec 14, 2016

This seems like a reasonable request. I'd accept a pull request for this.

@nex3 nex3 added type-enhancement A request for a change that isn't a bug contributions-welcome Contributions welcome to help resolve this (the resolution is expected to be clear from the issue) labels Dec 14, 2016
@rwrozelle
Copy link

I went back and looked at the sample code I provided and it had problems with encoding, below is much closer to original code. Is https://github.com/dart-lang/sdk/wiki/Contributing still the correct approach for making a patch contribution? If so, I'd be happy to give it a try and help by making this enhancement.

  String mapToQuery(Map<String, Object> map, {Encoding encoding}) {
    var pairs = <List<String>>[];

    void iterateMapEntry(key, value) {
      if (value is List) {
        value.forEach((subvalue) =>
            pairs.add([Uri.encodeQueryComponent(key, encoding: encoding),
            Uri.encodeQueryComponent(subvalue, encoding: encoding)]));
      }
      else {
        pairs.add([Uri.encodeQueryComponent(key, encoding: encoding),
        Uri.encodeQueryComponent(value, encoding: encoding)]);
      }
    }

    map.forEach(iterateMapEntry);

    return pairs.map((pair) => "${pair[0]}=${pair[1]}").join("&");
  }

@nex3
Copy link
Member

nex3 commented Dec 15, 2016

You can just send a pull request like normal—our bot will guide you through the process of signing the CLI if haven't yet.

@rwrozelle
Copy link

rwrozelle commented Dec 16, 2016

@nex3,

I just opened dart-lang/sdk#28142 since the following test fails:

    test('can be set with a value that is a List<String> and bodyFields returns value as list', () {
      var request = new http.Request('POST', dummyUrl);
      request.bodyFields = {'hello': ['world', 'solar system', 'galaxy', 'universe']};
      expect(request.bodyFields, equals({'hello': ['world', 'solar system', 'galaxy', 'universe']}));
    });

due to Uri.splitQueryString only returning the last value if the parameter is entered multiple times in the query string. I can create a local 'splitQueryString' method in utils.dart. Would that be acceptable for now in the pull request and then removed if my dart-lang/sdk#28142 request is resolved?

@nex3
Copy link
Member

nex3 commented Dec 19, 2016

@rwrozelle Yeah, that's fine.

@276562578
Copy link

I met this problem too during programing , did it fix now ?

@nex3
Copy link
Member

nex3 commented Jun 2, 2017

Not yet—I'm still working on hashing out a multi-map proposal.

@chunlea
Copy link

chunlea commented Nov 28, 2018

Any update for this?

@ElZombieIsra
Copy link

I encountered this error today trying to send this map as a body
{queryInput: {textInput: {text: Hola, cómo estás, languageCode: es}}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributions-welcome Contributions welcome to help resolve this (the resolution is expected to be clear from the issue) type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants