【问题标题】:Http post in DartDart 中的 Http 帖子
【发布时间】:2023-04-10 09:05:01
【问题描述】:

您好,我正在研究如何在 collect2.com 上发布数据或在 dart 中发布数据 我不知道如何发送令牌到服务器

import 'package:http/http.dart' as http;
import 'dart:io';

Future<http.Response> requestMethod() async {
final queryParameters = {
"action": "opened",
"issue": {"name": "Hello", "number": 1347},
"id": 1296269,
"full_name": "octocat/Hello-World",
"owner": {"login": "octocat", "id": 1}

};

  final uri = Uri.https(
  'collect2.com',
  '/api/xxxtokenxxx/datarecord/', queryParameters);


  final response = await http.post(uri, headers: {
  HttpHeaders.contentTypeHeader: 'application/json',
  HttpHeaders.acceptHeader: 'application/json'
      });

  return response;

}

它返回如下错误:

未处理的异常: 类型 '_InternalLinkedHashMap' 不是类型 'Iterable' 的子类型

卷曲:

curl -d '{"action": "opened", "issue": {"name": "Hello",      "number": 1347}, "repository": {"id": 1296269, "full_name":     "octocat/Hello-World", "owner": {"login": "octocat", "id": 1}}}' \
https://collect2.com/api/xxxtokenxxx/datarecord/ \
-H "Content-Type: application/json

【问题讨论】:

    标签: json api http dart collect


    【解决方案1】:
        import 'package:http/http.dart' as http;
        import 'dart:convert';
    
        Future<http.Response> send() {
        return http.post(
           Uri.parse(
              'https://collect2.com/api/token/datarecord/'),
       headers: <String, String>{'Content-  Type':'application/json;charset=UTF-8'},
          body: jsonEncode(<String, String>{
            "action": "opened",
          }),
        );
        }
    

    【讨论】: