我正在尝试使用上传产品映像。他们的网站(https://developers.squarespace.com/commerce-apis/upload-product-image)上的例子是在cURL中。这就是我用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,
}
);不幸的是,当我发送这个请求时,没有发送任何文件。
{
type: 'INVALID_REQUEST_ERROR',
subtype: null,
message: "Expected exactly one file part named 'file', but found none.",
details: null,
contextId: 'AWBREYTOOZEWWRIESHRE'
}解决此错误的最佳方法是什么?
发布于 2022-11-14 17:19:14
# 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命令中所指出的,您需要设置一个文件名。
// Set filename by providing a string for options
form.append("file", fs.createReadStream("./images/" + data._id + ".png"), 'myfilename.png');有关表格数据文件方法的更多详细信息,请参见.append
https://stackoverflow.com/questions/74435097
复制相似问题