Flutter : Post request with credentials

2019-04-16 18:59发布

问题:

How can I make post requests from flutter.I need to authenticate a user with his email address and password. Please help

tried with the following code

 http.post(url, body: {"email": "email", "password": "password"})
      .then((response) {
    print("Response status: ${response.statusCode}");
    //print("Response body: ${response.body}");
  });

getting "firewall read failed or timeout" error

回答1:

I use

import 'dart:async' show Future;
import 'dart:io'
    show HttpClient, HttpClientBasicCredentials, HttpClientCredentials;

import 'package:http/http.dart' show BaseClient, IOClient;

typedef Future<bool> HttpAuthenticationCallback(
    Uri uri, String scheme, String realm);

HttpAuthenticationCallback _basicAuthenticationCallback(
        HttpClient client, HttpClientCredentials credentials) =>
    (Uri uri, String scheme, String realm) {
      client.addCredentials(uri, realm, credentials);
      return new Future.value(true);
    };

BaseClient createBasicAuthenticationIoHttpClient(
    String userName, String password) {
  final credentials = new HttpClientBasicCredentials(userName, password);

  final client = new HttpClient();
  client.authenticate = _basicAuthenticationCallback(client, credentials);
  return new IOClient(client);
}
final http =
    createBasicAuthenticationIoHttpClient(_config.userName, _config.password);

http.get(...)


标签: dart flutter