Home > Enterprise >  Http post in Dart
Http post in Dart

Time:12-19

Hi I'm figuring out how post data on collect2.com or post data in dart I don't know how to send token to server

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;

}

it return error like this:

Unhandled exception:
type '_InternalLinkedHashMap<String, Object>' is not a subtype of type                                                        'Iterable<dynamic>'

Curl :

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

CodePudding user response:

    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",
      }),
    );
    }
  • Related