如何从flutter发出帖子请求。我需要使用他的电子邮件地址和密码对用户进行身份验证。请帮忙
尝试使用以下代码
http.post(url, body: {"email": "email", "password": "password"})
.then((response) {
print("Response status: ${response.statusCode}");
//print("Response body: ${response.body}");
});
获取"防火墙读取失败或超时"错误
答案 0 :(得分:2)
我用
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(...)