如何在 Flutter 中向 dio http 请求添加标头

时间:2021-05-20 17:07:23

标签: flutter http put dio

我有一个将文件发送到服务器的放置请求,它需要一个令牌作为标头的一部分。请问如何在使用 Dio().put 提出请求时将此令牌添加到标头中?

 Future<String> sendImage(File file) async {
    String token = storage.read('token');
    String fileName = file.path.split('/').last;
    FormData formData = FormData.fromMap({
      "CustomerProfilePicture":
          await MultipartFile.fromFile(file.path, filename: fileName),
    });
    Response response =
        await Dio().put(
            '$BASE_URL/Customers/Picture', data: formData);
    return response.statusMessage;
  }

2 个答案:

答案 0 :(得分:1)

您可以通过对请求使用 options 属性来传递标头:

await Dio().put(
  '$BASE_URL/Customers/Picture',
  data: formData,
  options: Options(
    headers: {
      ...
    }
  ),
);

有关详细信息,请查看 documentation

答案 1 :(得分:1)

这是将标头放入 DIO 请求的方法:

await Dio().put('$BASE_URL/Customers/Picture',
    data: formData,
    options: Options(
      headers: {"key": "value"},
    ));
相关问题