Can't figure out how to upload Attachments using node-asana

I try to upload an attachment to a task using the node-asana package.
I tried a few different things and nothing works.

  • using the raw data:

client.attachments
.dispatchPost("/tasks/{{gid}}/attachments", { file: {{raw buffer date}} })

i got the error: ‘file: Missing filename in Content-Disposition header’

client.attachments
.dispatchPost("/tasks/{{gid}}/attachments", { file: {{raw buffer date}} },
{})

  • List item

then I tried to set the Header like this:

client.attachments.dispatchPost("/tasks/{{gid}}/attachments",
{ file: {{raw buffer date}} },
{ headers: ‘Content-Disposition: form-data; name=“file”; filename=“test.txt”’}
)

i still got the error: ‘file: Missing filename in Content-Disposition header’

then i tried the multipart-form-data approach:

client.attachments.dispatchPost("/tasks/{{gid}}/attachments",
{ file:
`–{{boundary}}
Content-Disposition: form-data; name=“file”; filename=“test.txt”
Content-Type: text/plain

{{file content as string}}

–${boundary}–`
},
{ headers: ‘‘Content-Type’: 'multipart/form-data; boundary={{boundary}}’}
)

then i got the error: ‘The request body does not conform to the specification for multipart/form-data encoding.’,

i tried making the same request without the content-type header:

client.attachments.dispatchPost("/tasks/{{gid}}/attachments",
{ file:
`–{{boundary}}
Content-Disposition: form-data; name=“file”; filename=“test.txt”
Content-Type: text/plain

{{file content as string}}

–${boundary}–`
}
)

then i got the error: ‘file: File is not an object,

I would like to see a exact example how a correct body with ‘multipart/form-data’ encoding should look like.

Hi,
I have seen several post about this topic in the community, did you look for them?

yes, the posts I found were not really useful. I could not make it work.

Sorry I don’t know, I would just be reading existing posts like

@Phil_Seeman any idea?

I already tired this approach and it was not working

I’m afraid I haven’t done any work with attachments, I also don’t have any light to shed beyond what’s here in the forum.

@Ross_Grambo, can you add anything perhaps?

@Ivan_Hofer,

A super helpful debugging tool for me has been in dispatcher.js, you can change line 3 from

var request = require('request');

to

var request = require('request');
request.debug = true; 

And that will show you the actual request you’re making, which is extremely helpful for debugging. There may be a way to enable debugging without editing library files, but I’m unaware of it.

Anyway, I’m able to create an attachment with this:

var params = {
    method: 'POST',
    url: "https://app.asana.com/api/1.0/tasks/1234567890/attachments",
    formData: {
        file: fs.createReadStream('test.png')
    },
    headers: {
        "Content-Type": "multipart/form-data"
    },
};
client.dispatcher.dispatch(params, {});

Let me know if this works for you! :slight_smile:

Best,
Ross

2 Likes

Thanks @Ross_Grambo, super useful advice!

1 Like

Thanks for the detailed example.
Now the asana-endpoint seems to accept the request I try to send.

but now I get the error: “socket hang up”

i used the exact same piece of code like you mentioned. i only changed the path and the task-gid.

do you have any plan why I get this error message?

@Ivan_Hofer, you should try to recreate the request in postman, insomnia, or curl and see if you still get this issue to confirm it’s an issue with our API. Just based off that error I don’t have much insight.

The error might be due to attachment size. I believe our limit is currently 25MB.

I tested the attachment endpoint in postman and there it works. Using the same file and the code you suggested, I could not successfully send the request on my local nodeJS server.

Hey @Ivan_Hofer,

It sounds like it’s specific to your code or environment. I don’t think I’ll be too helpful as I’m unable to reproduce it on my end.

It looks like that error is a generic node request error and there’s a few issues it could be on some stack overflow questions.

You could try setting a longer timeout, as suggested here. You may have to edit the files in the asana package to do this.

If you’re able to find the issue, you or I can PR it into the asana-node repo if it makes sense.

I found the problem. I am using OAuth authentication. But the file-upload only works when I’m authenticated over a personal-access-token. Is this a intended behaviour?

No that is not expected behavior. Could you confirm that in postman? Get your access_token for oauth and move to postman to test it.

Also, try it a handful of times as well. Our attachment endpoint will sometimes fail, so it might have just been one of these cases.

It works in Postman. It is a problem with the asana npm package. I copied the same options from the request debug output and made that same call using the pure request npm package. There it works. I tried it now a few times and the only requests failing are these using the asana npm package