Hello - I’m receiving a Bad Request response while uploading a file to a task via the createAttachmentForObject method in my Node API. The file is an xlsx. I can’t upload those to this ticket, so I’m uploading a screen shot of the contents of the file. I took the effort to strip it down to a non-proprietary sample of the original spreadsheet, but this small sample is still causing the issue. If I remove certain rows from this sheet, it does work. I experimented with adding/removing rows to make it work with mixed success. But, the sheet as it is (in the screenshot) fails every time. We’re actually receiving reports of several attachment failures. This is the first one I’ve investigated. Here is the code in our API that’s attempting to upload the file. It’s worth mentioning, I’m able to manually upload the file using the task screen in the Asana web interface.
I have not been able to reproduce the issue with the file you sent me. Can you share the details of the error you are seeing? Does the file fail to attach? Or does the file attach to the task but then is later corrupt or unusable?
It doesn’t attach the file. In the code above, it’s failing on the createAttachmentForObject() call. Here is a screenshot of the error in the termal when running from localhost
Hmm… that’s not logging the actual error message returned by the API on why the call is failing. Are you able to reproduce the error on your machine and get more details?
Also, can you describe how your integration works? Does someone upload a file to your web app and then you’re taking that file and attaching it to a task? If so, I wonder if it might be an encoding issue. More details on what your integration does would be helpful!
That’s basically the flow. But, for the purposes of recreating the error, I’m simply just using fs.createReadStream to read in a file that already exists locally on my PC. So, I can replicate it with a simple file upload.
Here’s the readout of text, message and stack from the error object.
Again… this could totally be something on my end. I thought about using a formData.append, but that’s not the method used in Asana’s API documentation, so I haven’t even tried it.
Can you try with the following code snippet (I tested with node-asana v3.0.8):
Remember to replace the following placeholders with your own value:
<YOUR_ASANA_PERSONAL_ACCESS_TOKEN>
<YOUR_TASK_GID>
const Asana = require('asana');
const fs = require("fs");
let client = Asana.ApiClient.instance;
let token = client.authentications['token'];
token.accessToken = "<YOUR_ASANA_PERSONAL_ACCESS_TOKEN>";
const fileName = "TestFile.xlsm";
async function uploadAttachment() {
let attachmentsApiInstance = new Asana.AttachmentsApi();
let opts = {
'file': fs.createReadStream(fileName),
'parent': "<YOUR_TASK_GID>",
'name': fileName,
};
try {
let attachmentResponse = await attachmentsApiInstance.createAttachmentForObject(opts);
console.log('Attachment Response:' + JSON.stringify(attachmentResponse.data, null, 2));
} catch (error) {
console.error('Error: ' + error);
}
}
uploadAttachment();
Things to make sure/try:
The Personal Access Token you are using is from a user that has access to the task that you are trying to upload the file to
if fs.createReadStream(fileName), doesn’t work it’s worth downloading a node package that supports reading the type of file you are trying to upload EX: xlsx
value for parent is a string
value for name is a string
Also, try the following curl command if you have curl on your computer:
It looks like you’re testing this from a Windows PC. Can you tell me what version of Windows and what version of Node.js you are using? From the command prompt:
It does look like Node v16 is the culprit. If I install and switch to Node v16, I am able to reproduce the error you are seeing on both Windows and MacOS. Node v16 end-of-life (EOL) was reached September 2023, which means it is no longer supported with bug and security fixes. If I install and switch to Node v18 or v20, the Asana SDK posts the attachment without error.
Hopefully you are able to upgrade to Node v18 or v20. I use and recommend nvm for managing Node installations. There is a version of nvm for Windows.
I know one of the reasons Node v16 reached EOL sooner than the typical long-term support (LTS) schedule is due to a deprecated OpenSSL dependency. That might have something to do with the error POST-ing to the API endpoint.