0

How do you correctly add this kind of query parameters to a Dart http get request. This is my code

String? token;
  String? baseUrl = 'vps';
  String? path = 'web/session';

Map<String, dynamic> query ={
      "jsonrpc": "1.0",
      "method": "call",
      "id": "2",
      "params": {
        "login": username,
        "password": password,
        "db": "dev.sf.com",
        "context": {}
      }
    };

Uri uri = Uri.http(baseUrl.toString(), '$path/$endPath');
    if (query != null) {
      uri = Uri.http(baseUrl.toString(), '$path/$endPath', query);
    }
    return http.get(uri, headers: {
      'Authorization': 'Bearer $token',
      'Accept': 'application/json',
    });

but i get this error

 [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type '_InternalLinkedHashMap<String, Object>' is not a subtype of type 'Iterable<dynamic>'
alabiboo
  • 53
  • 1
  • 10
  • On what line do you get the error? – Frankdroid7 Aug 27 '22 at 21:35
  • See the documentation for the [`Uri` constructor](https://api.dart.dev/stable/dart-core/Uri/Uri.html), which applies to its other constructors as well: "When `queryParameters` is used ... A value in the map must be either a string, or an `Iterable` of strings..." The values for the `"params"` key cannot be a nested `Map`. – jamesdlin Aug 27 '22 at 21:37
  • on this line ```uri = Uri.http(baseUrl.toString(), '$path/$endPath', query);``` – alabiboo Aug 27 '22 at 21:37
  • Does this answer your question? [How do you add query parameters to a Dart http request?](https://stackoverflow.com/questions/52824388/how-do-you-add-query-parameters-to-a-dart-http-request) – Robert Sandberg Aug 27 '22 at 21:41
  • @jamesdlin the api i am using forces me to use the param key with nested Map – alabiboo Aug 27 '22 at 21:45
  • @RobertSandberg there is a difference in the param key in my code – alabiboo Aug 27 '22 at 21:48
  • And how does the API expect the nested map to be encoded to the URL? Examples, please. Are you sure these should be *query* parameters, which get encoded into the URL? – jamesdlin Aug 27 '22 at 21:49
  • @alabiboo, yeah and that is your problem – Robert Sandberg Aug 27 '22 at 21:50

1 Answers1

1

Try to send primitive types like int, string, boolean, etc. You send a Map as "params", this shouldnt work.

Map<String, dynamic> query ={
      "jsonrpc": "1.0",
      "method": "call",
      "id": "2",
      "login": username,
      "password": password,
      "db": "dev.sf.com",
    };

Alternative (and better imo): Make a post request and send it inside the body.

Ozan Taskiran
  • 2,922
  • 1
  • 13
  • 23