【发布时间】:2020-06-22 02:01:09
【问题描述】:
我正在尝试使用 Dio 插件在 Flutter 应用程序中执行 POST 请求。我有以下代码,但我似乎不知道为什么它不起作用。它将空数据发送到我的 API。
代码:
Future<String> sendRequest(String phone, int status) async {
String status = '';
print(sendConnectionUrl);
String bearerToken = await Endpoints.getBearerToken();
try {
Response response = await Dio().post(
'https://my.web.server/api',
data: json.encode({ "mobile": phone, "status": status }),
options: Options(
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ' + bearerToken
}
)
);
// The code doesnt even get here, it goes straight to the catch
print(response.toString());
print('status: ' + response.statusCode.toString());
var jsonData = json.decode(response.toString());
if (jsonData['error'] == '0') {
status = 'ok';
}
else {
status = 'failed';
}
}
catch (e) {
print('exception: ' + e.toString());
Future.error(e.toString());
}
return status;
}
但是在 POSTMAN 中发送请求是可行的。
【问题讨论】:
标签: android http flutter server