简体   繁体   English

如何在 flutter 中获取 POST api 的服务器响应

[英]how to get server response of a POST api in flutter

I am new to flutter and I am using mongodb to save the credentials from signup page.我是 flutter 的新手,我正在使用 mongodb 从注册页面保存凭据。 When tried to give credentials that already exists server shows a response - 'user already exits' this response was viewed in postman. I am able to get statusCode but I am unable to get the same response in flutter. below is my flutter code.当尝试提供已存在的凭据时,服务器会显示响应 -“用户已退出”,此响应在 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;
  }

To get the body response, use res.stream.bytesToString()要获取正文响应,请使用res.stream.bytesToString()

Complete code:完整代码:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM