给HTTP请求添加自定义头

1936次阅读  |  发布于5年以前

给HTTP请求添加自定义头

Use the headers argument to the function used to make an HTTP request. The example below adds a 'User-Agent' header to a get request.

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

main() async {
  var url = 'https://api.github.com/users/dart-lang/repos';
  var response =
      await http.get(url, headers: {'User-Agent': 'Dart/1.0 (My Dart client)'});

  List<String> repos = JSON.decode(response.body);
  var heading = 'Repository | Star count  | Fork count';
  print(heading);
  print(new List.filled(heading.length, '=').join());
  for (var repo in repos) {
    print("${repo['name']} | "
        "${repo['stargazers_count']} | "
        "${repo['forks_count']}");
  }
}

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8