【问题标题】:Dart: How to make GET request with bodyDart:如何使用正文发出 GET 请求
【发布时间】:2020-10-10 18:51:30
【问题描述】:

如何在 dart 中发送带有 body 的 GET 请求?

我尝试了 SO(thisthat 等)的所有食谱,但没有成功。

final queryParameters = {
  "id": "6767676767676",
  "device": "tatatata",
  "user": {
    "login": "login",
    "password": "pwd",
  }
};

这是复杂的 JSON,发送请求时出现错误: final uri = Uri.http('44.44.444.444:8080', 'get-answer', queryParameters);

Exception: type '_InternalLinkedHashMap<String, Object>' is not a subtype of type 'Map<String, String>'

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我认为您的queryParameters 不应包含对象。

    final queryParameters = {
        "id": "6767676767676",
        "device": "tatatata",
        "login" : "login",
        "password" : "password",
    };
    

    【讨论】:

    • 因合同原因。我无法从参数中删除对象。
    【解决方案2】:

    你试过了吗:

    var response = await get(url);
    var fetchedData = json.decode(response.body);
    

    fetchedData 将采用 json 地图格式

    【讨论】:

      【解决方案3】:

      答案实际上是 Flutter 在exception message 的这一部分中建议的:

      '_InternalLinkedHashMap' 不是'Map' 类型的子类型

      什么意思?

      这意味着,您的一个变量'_InternalLinkedHashMap<String, Object>',而flutter希望您将其更改为'Map<String, String>'类型

      您应该将queryParameters 中的密钥之一设为此'Map<String, String>' 类型

      如何?

      1。导入 Dart 转换

      通过导入dart:convert,我们将拥有jsonEncode 方法

      
          import 'dart:convert'; // put this at top of your file
      
      

      2。将嵌套值转换为字符串

      
          final nestedValue = {
              "login": "login",
              "password": "pwd",
            };
      
          String stringValue = jsonEncode(nestedValue); 
      

      3。把它放回你的主要参数

      
          final queryParameters = {
            "id": "6767676767676",
            "device": "tatatata",
            "user": stringValue
          };
      
          final uri = Uri.http('44.44.444.444:8080', 'get-answer', queryParameters);
      

      最终代码

      这将是您的最终代码,如下所示:

      
          import 'dart:convert'; // put this at top of your file
      
      
          final nestedValue = {
              "login": "login",
              "password": "pwd",
            };
      
          String stringValue = jsonEncode(nestedValue); // add this
      
          final queryParameters = {
            "id": "6767676767676",
            "device": "tatatata",
            "user": stringValue
          };
      
          final uri = Uri.http('44.44.444.444:8080', 'get-answer', queryParameters);
      

      【讨论】:

      • nope((( 现在参数成功了,但我得到了错误:Response body: {..."status":400,"error":"Bad Request","message":"Required request body is missing: 我可以意识到queryParameters 并不是真正的body
      • 当然,这是不同的问题。我认为您使用 JSON 端点对吗?它与您现在使用的不同。你最好使用包 dio。但是如果你发现你之前的问题现在已经解决了,Exception: type '_InternalLinkedHashMap' is not a subtype of type 'Map',你应该接受这个作为答案,然后它会自动关闭。
      • 谢谢,你是对的,你的建议对 Map<...> 很有帮助。我也尝试过 dio,它无助于解决我的“GET with body”问题。将挖掘来源))
      • 哦,我明白了,以前我遇到过这个问题。我不得不将 Content-type Application/json 放在标题上。并将 {} 放在身体上
      【解决方案4】:

      我建议您使用http package,它非常适合执行 http 请求。 在发送数据之前,请确保使用 jsonEncode 函数将映射编码为 json 格式,并从 GET 请求中获取数据,使用 jsonDecode 函数从 json 格式解码请求的正文。

      • 这些函数是从dart:convert 包中导入的
      //import the packages
      import 'package:http/http.dart' as http;
      import 'dart:convert';
      
      //other code here
      
      final queryParameters = jsonEncode( {
      "id": "6767676767676",
      "device": "tatatata",
      "login" : "login",
      "password" : "password",
      });
      
      //we send the data to the server with the post method
      await http.post("44.44.444.444:8080",queryParameters);
      
      similarly for retrieving data:
      
      final res = http.get("getUrl");
      
      //if the request is successful we get the data
      if(res.statusCode == 200)
          final data = jsonDecode(res.body);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-15
        • 1970-01-01
        • 2011-06-18
        • 1970-01-01
        相关资源
        最近更新 更多