【问题标题】:How to add headers to dio http request in Flutter如何在 Flutter 中向 dio http 请求添加标头
【发布时间】:2021-05-20 17:07:23
【问题描述】:

我有这个将文件发送到服务器的 put 请求,它需要一个令牌作为标头的一部分。请问如何在使用 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;
  }

【问题讨论】:

    标签: flutter http put dio


    【解决方案1】:

    您可以使用options 属性为您的请求传递标头:

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

    更多信息,请查看documentation。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-22
        • 1970-01-01
        • 1970-01-01
        • 2017-12-27
        • 1970-01-01
        • 2021-08-13
        相关资源
        最近更新 更多