http icon indicating copy to clipboard operation
http copied to clipboard

How I can get response data when calling multipart post request ?

Open aniketsongara opened this issue 3 years ago • 3 comments

Sample code is here

 final request = http.MultipartRequest('POST', Uri.parse("your_server_url"));
          request.files.add(http.MultipartFile.fromBytes("file", file.bytes, filename: file.name)); //your server may require a different key than "file"
          final response = await request.send();

// Now I want to get response data, How can I get it ?

if (response.statusCode == 200) {
         print("Response : ${response.body}"); // here I want to print the response.
          }

aniketsongara avatar May 17 '21 13:05 aniketsongara

@aniketsongara you can use like this: final respStr = await response.stream.bytesToString();

then respStr will contain the "response"

steinmetz avatar May 18 '21 15:05 steinmetz

@aniketsongara or you can use this: http.Response response = await http.Response.fromStream(await request.send()); ( json.decode(response.body) as Map<String, dynamic>)

edwinmrb avatar Aug 22 '22 05:08 edwinmrb

var response = await request.send(); var responseData = await response.stream.bytesToString(); final decodedMap = json.decode(responseData);

The decodedMap contains response the "json format of response"

DhleimaMohamed avatar Jun 23 '23 11:06 DhleimaMohamed