首页 > 解决方案 > Flutter:MultipartRequest返回403 Forbidden ... CURL工作正常

问题描述

我尝试以下 CURL 命令:

    -X PUT \
    -H 'Content-Type: application/octet-stream' \
    --upload-file ${file_name} \
    ${url}

将文件上传到 Google 存储 ( https://storage.googleapis.com/...?signature=...&... )。这工作得很好。

但是,当我尝试使用以下 Flutter 代码执行此操作时 - 我收到错误 403(禁止)的响应:

  Future<http.Response?> uploadVideo(
      {required String uploadURL, required filePath}) async {
    try {
      ByteData bytes = await rootBundle.load(filePath);

      var data =
          http.MultipartFile.fromBytes('file', bytes.buffer.asInt64List());
      Uri uri = Uri.parse(uploadURL);

      var request = http.MultipartRequest(
        'PUT',
        uri,
      );

      request.files.add(data);
      request.headers.addAll({
        HttpHeaders.contentTypeHeader: 'Content-Type: application/octet-stream',
      });

      http.StreamedResponse response = await request.send();
      return await http.Response.fromStream(response);
    } on SocketException catch (e) {
      showNoInternetError(e);
   }

知道为什么会这样吗?uploadURL 与我通过 CURL 发送的相同(很长的字符串,带有签名和 & 符号等)。

非常感谢!!!<3

标签: flutter

解决方案


推荐阅读