首页 > 解决方案 > 如何使用 dart 显示 http 请求的响应正文?

问题描述

我有一个卷曲请求:

curl \
  -F 'filename=@<IMAGE_PATH>' \
  -F 'access_token=<ACCESS_TOKEN>' \
  https://graph.facebook.com/v<API_VERSION>/act_<AD_ACCOUNT_ID>/adimages

我在飞镖中转换成这个请求:

final formData = {'filename': '@$path', 'access_token': ACCESS_TOKEN};

http.post(
  'https://graph.facebook.com/v8.0/act_$AD_ACCOUNT_ID/adimages',
  body: formData,
).then((http.Response response) {
  if (response.statusCode == HttpStatus.ok) {
    print("success");
    // print(response.statusMessage);
    // print(response.headers);
    print(response.body);
  } else {
    throw Exception('Failed to create Ads.');
  }

我正确发送了请求,因为我的响应类型为 200,但我想获取此请求的响应正文。当我尝试使用命令response.body结果是{} 里面没有任何东西。所以我想知道如何在知道 curl 请求有效并在 cmd 提示符下尝试时显示信息的情况下显示响应正文。我准确地说,我也尝试使用 Dio 包,但它返回了相同的结果(内部没有数据的赞誉)。

这是 curl 命令中响应的捕获:

在此处输入图像描述

标签: curldart

解决方案


推荐阅读