Flutter网络请求-进行认证请求


为了从更多的Web服务获取数据,有些时候您需要提供授权。有很多方法可以做到这一点,但最常见的可能是使用HTTP header中的Authorization

添加 Authorization Headers

http : https://pub.dartlang.org/packages/http package提供了一种方便的方法来为请求添加headers。您也可以使用dart:iopackage来添加。

Future<http.Response> fetchPost() {
  return http.get(
    'https://jsonplaceholder.typicode.com/posts/1',
    // Send authorization headers to your backend
    headers: {HttpHeaders.AUTHORIZATION: "Basic your_api_token_here"},
  );
}

完整的例子

这个例子建立在 从互联网上获取数据 之上 。

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

Future<Post> fetchPost() async {
  final response = await http.get(
    'https://jsonplaceholder.typicode.com/posts/1',
    headers: {HttpHeaders.AUTHORIZATION: "Basic your_api_token_here"},
  );
  final json = JSON.decode(response.body);

  return new Post.fromJson(json);
}

class Post {
  final int userId;
  final int id;
  final String title;
  final String body;

  Post({this.userId, this.id, this.title, this.body});

  factory Post.fromJson(Map<String, dynamic> json) {
    return new Post(
      userId: json['userId'],
      id: json['id'],
      title: json['title'],
      body: json['body'],
    );
  }
}