saiveda
saiveda

Reputation: 91

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. 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.

  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;
  }

Upvotes: 0

Views: 302

Answers (1)

OMi Shah
OMi Shah

Reputation: 6186

To get the body response, use 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;
  }

Upvotes: 1

Related Questions