【问题标题】:convert cURL command to http put request将 cURL 命令转换为 http put 请求
【发布时间】:2021-12-05 18:55:03
【问题描述】:

我需要转换以下 cURL 命令:

curl --request PUT \
--url https://storage.bunnycdn.com/storage/path/index.jpeg \
--header 'AccessKey: <AccessKey>' \
--header 'Content-Type: application/octet-stream' \
--data-binary @/home/path/to/index.jpeg

发送带有要上传到 CDN 的图像的 put 请求

到一个 https put 请求,特别是使用 --data-binary

我试过了:

   try {
                  var request = new http.MultipartRequest("PUT", uri);
                  request.headers.addAll({
                    'content-type': 'multipart/form-data',
                    'AccessKey': '<AccessKey>',
                  });

                  request.files.add(
                    await http.MultipartFile.fromPath(
                      'file',
                      _image.path,
                      filename: fileName,
                    ),

                  );

                  var response = request.send().then((value) => setState(() {
                        isLoading = false;
                      }));
                } catch (e) {
                  print(e);
                }

但不幸的是,文件到达 CDN 存储时格式错误。

如何使用 http put 请求将图像上传为 --data-binary

【问题讨论】:

    标签: javascript http curl put


    【解决方案1】:

    您可以使用 postman 将 curl 转换为某些语言,以 javascript postman 返回:

    var request = require('request');
    var options = {
      'method': 'PUT',
      'url': 'https://storage.bunnycdn.com/storage/path/index.jpeg',
      'headers': {
        'AccessKey': '<AccessKey>',
        'Content-Type': 'application/octet-stream'
      },
      body: '@/home/path/to/index.jpeg'
    
    };
    request(options, function (error, response) {
      if (error) throw new Error(error);
      console.log(response.body);
    });
    

    这只是一个示例,所以如果您需要更详细的内容,请执行此操作。

    【讨论】:

    • 我明白了,谢谢你告诉我邮递员可以将curl转换成其他语言,我会尽快尝试上面的代码并告诉你
    猜你喜欢
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2020-04-29
    • 1970-01-01
    • 2019-08-07
    • 2016-11-25
    • 2016-05-20
    相关资源
    最近更新 更多