[英]how to get server response of a POST api in flutter
我是 flutter 的新手,我正在使用 mongodb 從注冊頁面保存憑據。 當嘗試提供已存在的憑據時,服務器會顯示響應 -“用戶已退出”,此響應在 postman 中查看。我能夠獲得 statusCode,但無法在 flutter 中獲得相同的響應。下面是我的 flutter 代碼。
Future<String> uploadImage(filename) async {
var request = http.MultipartRequest('POST', Uri.parse(serverReceiverPath));
request.files.add(await http.MultipartFile.fromPath('file', filename));
var res = await request.send();
print(res.statusCode);
return null;
}
要獲取正文響應,請使用res.stream.bytesToString()
完整代碼:
Future<String> uploadImage(filename) async {
var request = http.MultipartRequest('POST', Uri.parse(serverReceiverPath));
request.files.add(await http.MultipartFile.fromPath('file', filename));
var res = await request.send();
print(res.statusCode); // status code
var bodyResponse = await res.stream.bytesToString(); // response body
print(bodyResponse);
return null;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.