使用用户基本身份验证创建FlatterJSON登录表单
在这一步上已经卡住了很长一段时间。有人可以告诉我如何发送请求,以便在按下“登录”按钮时创建具有基本身份验证的用户的新实例。我想了解如何知道如何安排标题来访问url数据库使用用户基本身份验证创建FlatterJSON登录表单,json,flutter,flutter-layout,flutter-test,Json,Flutter,Flutter Layout,Flutter Test,在这一步上已经卡住了很长一段时间。有人可以告诉我如何发送请求,以便在按下“登录”按钮时创建具有基本身份验证的用户的新实例。我想了解如何知道如何安排标题来访问url数据库 void Login() { final form = formKey.currentState; if (form.validate()) { form.save(); makePost(); } } second part my json methond
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'],
);
}
}
void登录(){
最终形式=formKey.currentState;
if(form.validate()){
form.save();
makePost();
}
}
第二部分我的json方法
Future makePost()异步{
字符串Username=“denisPos”;
字符串密码=“mov4b90wqphfghsg”;
最终响应=等待http.post('http://.60.4546.109:4520/postRequest',
标题:{HttpHeaders.authorizationHeader:'$Password:$Username:$url'});
final responseJson=json.decode(response.body);
return Post.fromJson(responseJson);
}
班岗{
最终字符串电话;
最终字符串密码;
最终管柱体;
Post({this.phone,this.password,this.body});
factory Post.fromJson(映射json){
回程站(
电话:json['phone'],
密码:json['password'],
body:json['body'],
);
}
}
如果您谈论的是基本身份验证,您可以使用以下代码:
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);
}
Future post()异步{
var url='1〕http://xxxxxxxxxxxxx;
字符串密码=“xxxxxxxxxxxxxx;
字符串用户名=”
var bytes=utf8.encode(“$username:$password”);
var-credentials=base64.encode(字节);
变量头={
“内容类型”:“应用程序/json”,
“授权”:“基本$credentials”
};
var requestBody=jsonEncode({'phone':phone,'pass':pass});
http.Response-Response=等待http.post(
url,body:requestBody,headers:headers);
var responseJson=json.decode(response.body);
打印(Utf8Codec().decode(response.bodyBytes));
打印(“正文:+responseJson”);
}
谢谢chem,它真的很有效。我只需将“Accept”:“application/json”更改为“setContentType”:“application/json”
http.Response response = await http.get(url, headers: headers);
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);
}