我正在尝试通过flutter发布到api,但这给了我这个错误:
I/flutter ( 5558): <!DOCTYPE html>
I/flutter ( 5558): <html lang="en">
I/flutter ( 5558): <head>
I/flutter ( 5558): <meta charset="utf-8">
I/flutter ( 5558): <title>Error</title>
I/flutter ( 5558): </head>
I/flutter ( 5558): <body>
I/flutter ( 5558): <pre>Cannot POST /login</pre>
I/flutter ( 5558): </body>
I/flutter ( 5558): </html>
I/flutter ( 5558): POST http://192.168.1.5:5000/login
我的代码:
void create() async {
Dio dio = Dio();
var response = await http.post('http://192.168.1.5:5000/login',headers: {
"Accept":"application/json",
},body: {
"username": "${usernameController.text}",
"password": "${passwordController.text}"
});
print(response.body);
print(response.request);
}
任何人都可以帮助plz吗? ,此错误非常奇怪
答案 0 :(得分:0)
您想使用发布请求来发布JSON数据,但您要向body
参数传递一个飞镖对象Map<String, String>
。
因此,您需要将此地图转换为json字符串。使用内置的dart:convert包
import 'dart:convert'
void create() async {
final Map<String, String> jsonData = {
"username": "${usernameController.text}",
"password": "${passwordController.text}"
};
Dio dio = Dio();
var response = await http.post('http://192.168.1.5:5000/login',headers: {
"Accept":"application/json",
},body: json.encode(jsonData),
);
print(response.body);
print(response.request);
}