我正在尝试从 API 获取数据。我需要从身体传递价值。
signIn(String username,String password) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
var jsonResponse = null;
var response = await http.post("http://10.0.2.2:5554/api/login/login",
headers : {
"Content-type": "application/json",
"Accept": "application/json",
"charset":"utf-8"
},
body:{
"username": '$username' ,
"password": '$password'
});
Run Code Online (Sandbox Code Playgroud)
您传递的主体值是一张地图。
要使其正常工作,您可以将标头的Content-Type更改为:"application/x-www-form-urlencoded"
或者
将映射编码为字符串并将其传递给主体:
body: json.encode({"username": '$username', "password": '$password'});
Run Code Online (Sandbox Code Playgroud)
不要忘记import 'dart:convert';后一个选项。
| 归档时间: |
|
| 查看次数: |
4181 次 |
| 最近记录: |