使用用户基本身份验证创建Flutter Json登录表单

时间:2018-09-28 11:16:46

标签: json flutter flutter-layout flutter-test

在此步骤上已经停留了很长时间。有人可以告诉我如何发送请求,以便在按下“登录”按钮后创建具有基本身份验证的新用户实例。我很想了解如何知道标题是如何配置的以访问url数据库

     void Login() {
    final form = formKey.currentState;
    if (form.validate()) {
      form.save();
      makePost();
    }
  }

    second part my json methond

     Future<Post> makePost() async {
    String Username = "denisPos";
    String Password = "moV4b90WqpHfghghsg";
    final response = await http.post('http://.60.4546.109:4520/postRequest',
        headers: {HttpHeaders.authorizationHeader: '$Password:$Username:$url'});
    final responseJson = json.decode(response.body);

    return Post.fromJson(responseJson);
  }


    class Post {
  final String phone;

  final String password;
  final String body;

  Post({this.phone, this.password, this.body});

  factory Post.fromJson(Map<String, dynamic> json) {
    return Post(
      phone: json['phone'],
      password: json['password'],
      body: json['body'],
    );
  }
}

2 个答案:

答案 0 :(得分:0)

如果您正在谈论基本身份验证,则可以使用以下代码:

Future<Post> makePost() async {
  String username = "denisPos";
  String password = "moV4b90WqpHfghghsg";
  var bytes = utf8.encode("$username:$password");
  var credentials = base64.encode(bytes);
  var headers = {
    "Accept": "application/json",
    "Authorization": "Basic $credentials"
  };
  var url = ...
  var requestBody = ....
  http.Response response = await http.post(url, body: requestBody, headers: headers);
  var responseJson = json.decode(response.body);

  return Post.fromJson(responseJson);
}

如果您要发送GET请求,则可以完全省略requestBody

http.Response response = await http.get(url, headers: headers);

答案 1 :(得分:0)

    Future<http.Response> post() async {
    var url = 'http://xxxxxxxxxxxxx;
    String password = "xxxxxxxxxxxxxxxx;
    String username = "
    var bytes = utf8.encode("$username:$password");


    var credentials = base64.encode(bytes);
    var headers = {
      "Content-Type": "application/json",
      "Authorization": "Basic $credentials"
    };

    var requestBody = jsonEncode({ 'phone': phone, 'pass': pass});

    http.Response response = await http.post(
        url, body: requestBody, headers: headers);
    var responseJson = json.decode(response.body);
    print(Utf8Codec().decode(response.bodyBytes));

    print("Body: " + responseJson);
  }