How to get response body with request.send() in dart

前端 未结 4 444
说谎
说谎 2021-02-04 02:30

I\'m doing an api request uploading an image with

var request = new http.MultipartRequest(\"POST\", uri);
var response = await request.send()

i

4条回答
  •  有刺的猬
    2021-02-04 02:35

    You can cast after first response, look :

    var postUri = Uri.parse("http://my-api.com/updatePhoto");
    var request = new http.MultipartRequest("POST", postUri);
    
    request.fields['user_id'] = user_id
    
    request.files.add(await http.MultipartFile.fromPath(
      'photo',
      myPhoto.absolute.path,
      contentType: new MediaType('application', 'x-tar'),
    ));
    
    request.send().then((result) async {
    
      http.Response.fromStream(result)
          .then((response) {
    
        if (response.statusCode == 200)
        {
          print("Uploaded! ");
          print('response.body '+response.body);
        }
    
        return response.body;
    
      });
    }).catchError((err) => print('error : '+err.toString()))
        .whenComplete(()
    {});
    

提交回复
热议问题