如何在dart中使用request.send()获取响应主体 [英] How to get response body with request.send() in dart
问题描述
我正在通过api请求上传图片,
I'm doing an api request uploading an image with
var request = new http.MultipartRequest("POST", uri);
var response = await request.send()
用飞镖飞镖.然后,我可以使用
in dart using flutter. I can then check the response code with for instance
if (response.statusCode == 200) {
print('ok');
}
通过其他调用,我也可以使用
with other calls I can also get the response body with
var result = response.body;
但是当使用request.send()时,我似乎无法找出如何获得响应正文结果.
however when using request.send() I can't seem to find out how to get the response body result.
非常感谢您的帮助或投入,谢谢!
Any help or input is very much appreciated, thanks!
推荐答案
I checked the docs for request.send I returns Future<StreamedResponse>
instead of Future<Response>
进一步挖掘 StreamedResponse ,我发现它 response.stream
,它是 ByteStream
Digging more for StreamedResponse I found that it response.stream
which is a ByteStream
这是您可以获取String响应的方法
Here is what you can do to get response in String
final response = await request.send();
final respStr = await response.stream.bytesToString();
在我的视觉观点中,如果要流式响应而不是已收集"响应,则仅应使用request.send.有关此处的dart流
In my opinoin you should only use request.send if you want streamed response instead of "collected" response. More about streams in dart here
这篇关于如何在dart中使用request.send()获取响应主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!