# Need Help Troubleshoooting Bad Request in createAttachmentForObject

**URL:** <https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410>\
**Category:** API & Developers\
**Created:** [2 August 2024 15:32 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410 "2024-08-02T15:32:09Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christine\_Hill2](https://luna1.co/8d76cd.png) [@Christine\_Hill2](https://forum.asana.com/u/Christine_Hill2)\
**Post date:** [2 August 2024 15:32 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410/1 "2024-08-02T15:32:09Z")

</div>

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.

> let opts = {  
> ‘file’: fs.createReadStream(safeFile),  
> ‘name’: attachment.SafeName,  
> ‘parent’: asanaID  
> };  
> await attachmentsApiInstance.createAttachmentForObject(opts).then(t =\> {  
> console.log(‘t’);  
> // fs.unlinkSync(safeFile)  
> }, (error) =\> {  
> console.log(‘err:’, error.response.body)  
> })

 ![Capture](https://us1.discourse-cdn.com/asana/original/3X/b/7/b79b2b060c1a89538a893bfddcbb23d1a22a15d6.png)

---

<div class="post-metadata">

**Author:** ![David\_Neal](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/david_neal/32/242025_2.png) [@David\_Neal](https://forum.asana.com/u/David_Neal)\
**Post date:** [5 August 2024 16:39 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410/2 "2024-08-05T16:39:32Z")

</div>

Hi Christine!

I’ll follow up with you in a direct message and help troubleshoot this issue!

---

<div class="post-metadata">

**Author:** ![David\_Neal](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/david_neal/32/242025_2.png) [@David\_Neal](https://forum.asana.com/u/David_Neal)\
**Post date:** [6 August 2024 16:21 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410/3 "2024-08-06T16:21:36Z")

</div>

Hi Christine!

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?

---

<div class="post-metadata">

**Author:** ![Christine\_Hill2](https://luna1.co/8d76cd.png) [@Christine\_Hill2](https://forum.asana.com/u/Christine_Hill2)\
**Post date:** [6 August 2024 17:24 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410/4 "2024-08-06T17:24:43Z")

</div>

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

 ![Screenshot 2024-08-06 102311](https://us1.discourse-cdn.com/asana/original/3X/3/4/3416f6eacff8484515a73508d390505791072f79.png)

---

<div class="post-metadata">

**Author:** ![David\_Neal](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/david_neal/32/242025_2.png) [@David\_Neal](https://forum.asana.com/u/David_Neal)\
**Post date:** [6 August 2024 19:01 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410/5 "2024-08-06T19:01:49Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![David\_Neal](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/david_neal/32/242025_2.png) [@David\_Neal](https://forum.asana.com/u/David_Neal)\
**Post date:** [6 August 2024 19:09 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410/6 "2024-08-06T19:09:24Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Christine\_Hill2](https://luna1.co/8d76cd.png) [@Christine\_Hill2](https://forum.asana.com/u/Christine_Hill2)\
**Post date:** [6 August 2024 20:29 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410/7 "2024-08-06T20:29:33Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/asana/original/3X/9/0/901857cfbce13b50bef45deebb9d8111fccaf210.png)

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.

---

<div class="post-metadata">

**Author:** ![Christine\_Hill2](https://luna1.co/8d76cd.png) [@Christine\_Hill2](https://forum.asana.com/u/Christine_Hill2)\
**Post date:** [6 August 2024 20:30 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410/8 "2024-08-06T20:30:16Z")

</div>

^ this trace doesn’t really tell me much… unless something is hidden in the gobbledygook.

---

<div class="post-metadata">

**Author:** ![John\_Vu](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/john_vu/32/97235_2.png) [@John\_Vu](https://forum.asana.com/u/John_Vu)\
**Post date:** [6 August 2024 20:48 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410/9 "2024-08-06T20:48:55Z")

</div>

@Christine_Hill2 ,

Can you try with the following code snippet (I tested with [node-asana v3.0.8](https://www.npmjs.com/package/asana/v/3.0.8)):

Remember to replace the following placeholders with your own value:

- `<YOUR_ASANA_PERSONAL_ACCESS_TOKEN>`
- `<YOUR_TASK_GID>`

```javascript
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:

1. 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
2. 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](https://www.npmjs.com/package/xlsx)
3. value for `parent` is a string
4. value for `name` is a string

* * *

Also, try the following `curl` command if you have `curl` on your computer:

```bash
curl --location 'https://app.asana.com/api/1.0/attachments' \
--header 'Content-Type: multipart/form-data' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer <YOUR_PERSONAL_ACCESS_TOKEN>' \
--form 'parent="<YOUR_TASK_GID>"' \
--form 'file=@"<PATH_TO_YOUR_TestFile.xlsx>"' \
--form 'name="TestFile.xlsx"'

```

This will help us determine if it’s an API issue or node client library issue

---

<div class="post-metadata">

**Author:** ![Christine\_Hill2](https://luna1.co/8d76cd.png) [@Christine\_Hill2](https://forum.asana.com/u/Christine_Hill2)\
**Post date:** [6 August 2024 21:34 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410/11 "2024-08-06T21:34:53Z")

</div>

> [@Christine\_Hill2](#):
>
> Using this code snippet, I’m still getting a Bad Request error. I even created a brand new file that was just 100 rows of the word “something” in each cell from columns A to O, and it still threw a Bad Request error.
> 
> Test File:  
> [https://tmpfiles.org/dl/10721147/somethingfile.xlsx](https://tmpfiles.org/dl/10721147/somethingfile.xlsx)
> 
> Like before… if I strip this file down to only 5 rows of “something,” it goes through no problem. I think the issue has to do with the file size.
> 
> Here’s the code in my function.
> 
> ```auto
> const fileName = `C:\\Home\\SomethingFile.xlsx`;
> let client = Asana.ApiClient.instance;
> let token = client.authentications['token'];
> token.accessToken = process.env.ASANA_BEARER_TOKEN;
> let attachmentsApiInstance = new Asana.AttachmentsApi();
> let opts = { 
> 'file': fs.createReadStream(fileName), 
> 'parent': "1207957667825704", 
> '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);
> }
> 
> ```

I can try a different file reader… but, I’d hate to have to use a bunch of different readers if I run into this issue with a different file type…

---

<div class="post-metadata">

**Author:** ![David\_Neal](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/david_neal/32/242025_2.png) [@David\_Neal](https://forum.asana.com/u/David_Neal)\
**Post date:** [6 August 2024 21:59 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410/12 "2024-08-06T21:59:37Z")

</div>

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:

```sh
node -v

```

---

<div class="post-metadata">

**Author:** ![Christine\_Hill2](https://luna1.co/8d76cd.png) [@Christine\_Hill2](https://forum.asana.com/u/Christine_Hill2)\
**Post date:** [6 August 2024 22:01 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410/13 "2024-08-06T22:01:20Z")

</div>

Windows 11 Enterprise (I actually just upgraded from 10 I think, and it didn’t work in 10 either).  
node v16.20.2

---

<div class="post-metadata">

**Author:** ![David\_Neal](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/david_neal/32/242025_2.png) [@David\_Neal](https://forum.asana.com/u/David_Neal)\
**Post date:** [7 August 2024 15:48 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410/14 "2024-08-07T15:48:40Z")

</div>

Hi @Christine_Hill2!

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](https://github.com/coreybutler/nvm-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.

---

<div class="post-metadata">

**Author:** ![Christine\_Hill2](https://luna1.co/8d76cd.png) [@Christine\_Hill2](https://forum.asana.com/u/Christine_Hill2)\
**Post date:** [7 August 2024 21:43 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410/15 "2024-08-07T21:43:13Z")

</div>

oh interesting. I can work to get our Node version updated. Hopefully it doesn’t send me too far into dependency hell 😉

---

<div class="post-metadata">

**Author:** ![Christine\_Hill2](https://luna1.co/8d76cd.png) [@Christine\_Hill2](https://forum.asana.com/u/Christine_Hill2)\
**Post date:** [12 August 2024 20:39 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410/16 "2024-08-12T20:39:29Z")

</div>

Upgrading to 18 fixed it!! Thanks so much you guys are the best!!!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/system/32/276370_2.png) [@system](https://forum.asana.com/u/system)\
**Post date:** [19 August 2024 20:39 UTC](https://forum.asana.com/t/need-help-troubleshoooting-bad-request-in-createattachmentforobject/880410/17 "2024-08-19T20:39:31Z")

</div>

Closed on 19 August 2024 20:39 UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
