我在向api发布信息后尝试获取响应,但是当我尝试打印该行为空时,响应(这是一个json)没有出现。有人可以帮我弄清楚我在做什么错吗?
我已经打印了状态代码,仅用于测试及其返回的500,这可能相关吗?
Future<void> _login() async {
Map<String, dynamic> newLogin = Map();
newLogin["user"] = _usuarioController.text.trimLeft();
newLogin["pass"] = _senhaController.text.trimLeft();
Map<String, String> headers = new Map<String, String>();
headers["Content-type"] = "application/json";
headers["Accept"] = "application/json";
int timeout = 2;
http.Response response = await http
.post('https://sistema.hutransportes.com.br/api/login.php',
headers: headers, body: jsonEncode(newLogin), encoding: utf8)
.timeout(Duration(seconds: timeout));
print(newLogin);
print(response.statusCode);
print(response.body); //Where the empty response comes
}
您的服务器不接受JSON-而是期望为x-www-form-urlencoded
,因此请勿尝试将帖子编码为JSON。
此请求获得200 OK:
Future<void> _login() async {
var form = <String, String>{
'user': 'abrev',
'pass': 'password',
};
var headers = <String, String>{'accept': 'application/json'};
final timeout = 2;
var response = await http
.post(
'https://sistema.hutransportes.com.br/api/login.php',
headers: headers,
body: form,
)
.timeout(Duration(seconds: timeout));
print(form);
print(response.statusCode);
print(response.body);
}