首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过Squarespace上传图像

通过Squarespace上传图像
EN

Stack Overflow用户
提问于 2022-11-14 16:48:30
回答 1查看 15关注 0票数 0

我正在尝试使用上传产品映像。他们的网站(https://developers.squarespace.com/commerce-apis/upload-product-image)上的例子是在cURL中。这就是我用javascript写的。

代码语言:javascript
运行
复制
const form = new FormData();
    form.append("file", fs.createReadStream("./images/" + data._id + ".png"));
    const send_img = await fetch(
      "https://api.squarespace.com/1.0/commerce/products/" + img_id + "/images",
      {
        method: "POST",
        headers: {
          Authorization: "Bearer " + process.env.SQUARESPACE_API_KEY,
          "User-Agent": "MYUSERAGENT",
          "Content-Type": "multipart/form-data",
        },
        body: form,
      }
    );

不幸的是,当我发送这个请求时,没有发送任何文件。

代码语言:javascript
运行
复制
{
  type: 'INVALID_REQUEST_ERROR',
  subtype: null,
  message: "Expected exactly one file part named 'file', but found none.",
  details: null,
  contextId: 'AWBREYTOOZEWWRIESHRE'
}

解决此错误的最佳方法是什么?

EN

回答 1

Stack Overflow用户

发布于 2022-11-14 17:19:14

代码语言:javascript
运行
复制
  # The cURL flag below automatically sets `name` to "file"
  # and `filename` to the specified filename.
  # If not using cURL, it’s strongly recommended to use a library that supports
  # `multipart/form-data` requests so the parameters below can be specified:
  #     `name` = "file"
  #     `filename` = "YOUR_FILENAME"
  # If `name` is not set to "file" and/or `filename` is not specified then the request fails

我认为,正如在cURL命令中所指出的,您需要设置一个文件名。

代码语言:javascript
运行
复制
// Set filename by providing a string for options
form.append("file", fs.createReadStream("./images/" + data._id + ".png"), 'myfilename.png');

有关表格数据文件方法的更多详细信息,请参见.append

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74435097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档