我正在尝试发送带有主体的DELETE请求,但是http.delete
不支持在请求中添加主体。
我已经在线找到了该解决方案,但是我想返回Response
而不是StreamedResponse
。我该如何实现?
static Future<http.Response> deleteFavorites({Map<String, int> body}) async {
UserRepository userRepository = UserRepository();
String token = await userRepository.storage.read(key: 'token');
final client = http.Client();
var response;
try {
response =
await client.send(http.Request("DELETE", Uri.parse(favoritesUrl))
..headers["access-token"] = token
..body = jsonEncode(body));
//
} finally {
client.close();
}
return response;
}
答案 0 :(得分:1)
您可以在Response类fromStream中使用静态方法,该方法采用StreamedResponse并返回所需的Future,只需将return更改为:
return Response.fromStream(response);
您可以检查文档https://pub.dev/documentation/http/latest/http/Response-class.html。
希望有帮助!