I'm using the Quickbooks API with the NodeJS SDK (oauth-jsclient) and I'm having issues trying to figure out how to upload an image to an Inventory Item. Per documentation (docs)it says to make a multipart/form-data post request to the endpoint https://sandbox-quickbooks.api.intuit.com/v3/company/${company_id}/upload?minorversion=70.
I'm doing all that and I'm also using the form-data library to handle the multipart/form-data to attach the image file. The issue I'm having is that I get an error response from the API saying Content length missing in request","Detail":"We cannot find the content-length header in the request.","code":"6020"}
Here is the relevant part of my code:
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 });
}
If I console log the headers this is what I get:
{
formHeader: {
'content-type': 'multipart/form-data; boundary=--------------------------348255206805762341115729'
}
}
Any ideas what I'm doing wrong? any help would be greatly appreciated.
So I think I figured it out.
Its due to the lack of support in handling multipart/form data in the oauth-jsclient library. According to this Github Issue
Not clear if they added the support since then, but I'm guessing they haven't due to the error I'm getting.