我正在将 Quickbooks API 与 NodeJS SDK (oauth-jsclient) 一起使用,并且在尝试弄清楚如何将图像上传到库存项目时遇到问题。根据文档 (docs)它说要向端点发出多部分/表单数据发布请求
https://sandbox-quickbooks.api.intuit.com/v3/company/${company_id}/upload?minorversion=70
。
我正在做所有这些,我还使用表单数据库来处理多部分/表单数据以附加图像文件。我遇到的问题是,我从 API 收到错误响应,内容为
Content length missing in request","Detail":"We cannot find the content-length header in the request.","code":"6020"}
这是我的代码的相关部分:
try {
if (images?.length) {
const image = images[0];
const imageFile = fs.createReadStream(image.rawInfo.path);
const form = new FormData();
form.append('file_content_01', imageFile, {
filename: image.rawInfo.filename,
contentType: image.rawInfo.mimetype,
});
form.append(
'file_metadata_01',
JSON.stringify({
AttachableRef: [
{
EntityRef: { type: 'Item', value: newQBProduct.Id },
},
],
ContentType: image.rawInfo.mimetype,
FileName: image.rawInfo.filename,
}),
{
contentType: 'application/json',
filename: 'file_metadata_01',
}
);
console.log({ formHeader: form.getHeaders() });
const imageResponse = await qbClient.makeApiCall({
url: `${process.env.QB_API_URL}/v3/company/${process.env.QB_COMPANY_ID}/upload?minorversion=69`,
method: 'POST',
headers: {
...form.getHeaders(),
},
body: form,
});
}
} catch (error) {
console.log({ ERROR_UPLOADING_IMAGE: error });
}
如果我控制台记录标题,这就是我得到的:
{
formHeader: {
'content-type': 'multipart/form-data; boundary=--------------------------348255206805762341115729'
}
}
你知道我做错了什么吗?任何帮助将不胜感激。
所以我想我已经明白了。
这是由于 oauth-jsclient 库中缺乏处理多部分/表单数据的支持。根据这个Github问题
不清楚他们是否从那时起就添加了支持,但我猜他们没有因为我收到的错误而添加支持。