为什么我输入了正确的凭据,但仍然得到无效的凭据?
Why am I still getting an invalid credential even if I enter the correct credential?
我正在使用 flutter 制作一个登录应用程序。我想用电子邮件和密码登录。如果响应是 200,它将转到另一个页面,如果不是,它将显示 "Invalid Credential"。我的问题是,即使我输入了正确的电子邮件和密码,我总是会收到无效的凭据。我想我的令牌有问题。我怎么解决这个问题?这是我的 post 人的回复。
Method: POST->Body->JSON
{
"email": "user.user@gmail.com",
"password": "123456"
}
//
{
"code": 0,
"message": "success",
"data": {
"Id": 121106,
"Name": "User 1",
"Email": "user.user@gmail.com",
"Token": "55ecaa9b-0a0a-4669-b234-c7e7c88f8f41"
}
}
//
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(10.0),
child: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
controller: emailController,
decoration: const InputDecoration(
labelText: "Email",
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.email)),
),
const SizedBox(height: 15,),
TextFormField(
controller: passController,
decoration: const InputDecoration(
labelText: "Password",
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.email)),
),
const SizedBox(height: 15,),
OutlinedButton.icon(onPressed: () {
login();
}, icon: const Icon(Icons.login, size: 18,), label: const Text("Login")),
],
)
)
),
),
);
}
//创建调用登录的函数postapi
Future<void> login() async {
if(emailController.text.isNotEmpty && passController.text.isNotEmpty) {
var headers = {"Content-type": "application/json"};
var myBody = {'email' : emailController.text, 'password' : passController.text,};
var response = await http.post(Uri.parse("url"),
headers: headers,
body: utf8.encode(json.encode( myBody )));
print(response.statusCode);
print(response.body);
if(response.statusCode == 200) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const SecondRoute()));
} else {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Invalid Credentials.")));
}
} else {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Blank Field Not Allowed")));
}
}
}
检查响应:
print(response.statusCode);
print(response.body);
设置正确的 headers,但 REST 端点可接受的 headers 可能会有所不同。您需要阅读 API 文档。
像这样的东西可能会起作用,但实际 content-type 可能因 REST 端点而异:
var headers = {"Content-type": "application/json"};
var response = await http.post(Uri.parse("http://restapi.com/api/login"),
headers: headers,
body: ({
'email' : emailController.text,
'password' : passController.text,
}));
此外,可能需要按照这些行对 body 进行编码:
var myBody = {
'email' : emailController.text,
'password' : passController.text,
}
...
body: utf8.encode(json.encode( myBody ))
我正在使用 flutter 制作一个登录应用程序。我想用电子邮件和密码登录。如果响应是 200,它将转到另一个页面,如果不是,它将显示 "Invalid Credential"。我的问题是,即使我输入了正确的电子邮件和密码,我总是会收到无效的凭据。我想我的令牌有问题。我怎么解决这个问题?这是我的 post 人的回复。
Method: POST->Body->JSON
{
"email": "user.user@gmail.com",
"password": "123456"
}
//
{
"code": 0,
"message": "success",
"data": {
"Id": 121106,
"Name": "User 1",
"Email": "user.user@gmail.com",
"Token": "55ecaa9b-0a0a-4669-b234-c7e7c88f8f41"
}
}
//
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(10.0),
child: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
controller: emailController,
decoration: const InputDecoration(
labelText: "Email",
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.email)),
),
const SizedBox(height: 15,),
TextFormField(
controller: passController,
decoration: const InputDecoration(
labelText: "Password",
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.email)),
),
const SizedBox(height: 15,),
OutlinedButton.icon(onPressed: () {
login();
}, icon: const Icon(Icons.login, size: 18,), label: const Text("Login")),
],
)
)
),
),
);
}
//创建调用登录的函数postapi
Future<void> login() async {
if(emailController.text.isNotEmpty && passController.text.isNotEmpty) {
var headers = {"Content-type": "application/json"};
var myBody = {'email' : emailController.text, 'password' : passController.text,};
var response = await http.post(Uri.parse("url"),
headers: headers,
body: utf8.encode(json.encode( myBody )));
print(response.statusCode);
print(response.body);
if(response.statusCode == 200) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const SecondRoute()));
} else {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Invalid Credentials.")));
}
} else {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Blank Field Not Allowed")));
}
} }
检查响应:
print(response.statusCode);
print(response.body);
设置正确的 headers,但 REST 端点可接受的 headers 可能会有所不同。您需要阅读 API 文档。
像这样的东西可能会起作用,但实际 content-type 可能因 REST 端点而异:
var headers = {"Content-type": "application/json"};
var response = await http.post(Uri.parse("http://restapi.com/api/login"),
headers: headers,
body: ({
'email' : emailController.text,
'password' : passController.text,
}));
此外,可能需要按照这些行对 body 进行编码:
var myBody = {
'email' : emailController.text,
'password' : passController.text,
}
...
body: utf8.encode(json.encode( myBody ))