Upload attachment with CURL

Hi everyone.

I am doing some API work right now and I am creating tasks and subtasks.
Now I need to add attachments from my form into the task.

$binary = file_get_contents($_FILES['file']['tmp_name'][$i]);
$fileName = $_FILES['file']['name'][$i];

$curl = curl_init();


        curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://app.asana.com/api/1.0/tasks/' . $taskGid . '/attachments',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => array('file' => $binary, 'resource_subtype' => 'asana', name => $fileName),
        CURLOPT_HTTPHEADER => array(
                    'Authorization: Bearer $token',
                    'Cookie: TooBusyRedirectCount=0; TooBusyRedirectCount=0;                                                                                          logged_out_uuid=54a01a0dd7857620640436eb9ab6e4e0' ),
        ));

$resp = curl_exec($curl);

This is what I have right now.

It does not upload the file at all as an attachment.
I have been looking through this forum, google and everything I could find, but I can’t find a solution that works here.

Hi,

I successfully did it using nodeJS, and it was haaaaarrrddd. I remember seeing discussion on the forum, let me find it.

Did you check out

?

Hi.

Yeah I did check those out. I have tried several things listed here and on stackoverflow, but it does not work.
I do not get any errors and the attachments does not get into Asana.

I am really happy to say it’s an interesting post to read. I learn new information from your article , you are doing a great job. Keep it up.

1 Like

Did you found ay solution? I was Facing the same.

I am back to square one, trying to upload a file from cURL. No luck so far.

Any luck here @Bastien_Siebman ?

I honestly can’t even get the Postman workspace from Asana to add an attachment.

I don’t think so, I played with it again but I think that’s Zapier that succeeded at uploading an attachment, not me.

Hey guys,

maybe try it like this:

curl -X POST https://app.asana.com/api/1.0/attachments \
  -H 'Content-Type: multipart/form-data' \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer <token>' \
  -F "file=@image.png;type=image/png" \
  -F "name=image.png" \
  -F "parent=<task-gid>"

In this example “image.png” ist the file path and “image/png” the MIME type.

So far I did not find a solution to pass a binary object directly instead of storing the file temporarily.

Hope this helps.

2 Likes