简体   繁体   English

如何将这种查询参数添加到 Dart http 请求中?

[英]How do you add this kind of query parameters to a Dart http request?

How do you correctly add this kind of query parameters to a Dart http get request.如何正确地将这种查询参数添加到 Dart http 获取请求中。 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>'

Try to send primitive types like int, string, boolean, etc. You send a Map as "params", this shouldnt work.尝试发送原始类型,如 int、string、boolean 等。您将 Map 作为“参数”发送,这不应该工作。

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.替代方案(和更好的 imo):发出帖子请求并将其发送到正文中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM