Skip to content

Instantly share code, notes, and snippets.

@dougblackjr
Last active April 13, 2023 13:47
Show Gist options
  • Save dougblackjr/2be6c114e80223d0e7398c635b489819 to your computer and use it in GitHub Desktop.
Save dougblackjr/2be6c114e80223d0e7398c635b489819 to your computer and use it in GitHub Desktop.
Clickup File Attachment via API
$yourApiToken = 'your-api-token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.clickup.com/api/v2/task/{task_id}/attachment?custom_task_ids=&team_id=");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
$filepath = '/path/to/file';
$file = curl_file_create($filepath, 'image/png', 'filename');
curl_setopt(
$ch,
CURLOPT_POSTFIELDS,
[
'attachment' => $file,
]
);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: {$yourApiToken}",
"Content-Type: multipart/form-data"
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
@tamara-m
Copy link

tamara-m commented Aug 25, 2022 via email

@rajnaviktor
Copy link

rajnaviktor commented Aug 26, 2022

I'm not sure, but your issue seems to be the file. Can you log the file path and make sure you have a file with that name in that location?

On Thu, Aug 25, 2022, 12:14 rajnaviktor @.> wrote: @.* commented on this gist. ------------------------------ This is what I am doing and it works @rajnaviktor https://github.com/rajnaviktor $url .= 'https://api.clickup.com/api/v2/task/'.$task['id'].'/attachment/'; $args = [ 'file_path' => $file_path, 'file_type' => $file_type, 'file_name' => $file_name ]; $file = curl_file_create($args['file_path'], $args['file_type'], $args['file_name'] ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, [ 'attachment' => $file ]); curl_setopt( $ch, CURLOPT_HTTPHEADER, [ "Content-Type: multipart/form-data", "Authorization:" . $api_token ] ); $response = curl_exec($ch); curl_close($ch); $clickup_push = json_decode( $response, true ); I try this, but the API response is always '{"err":"No attachment supplied","ECODE":"ATTCH_039"}' Can wampserver settings cause problems? — Reply to this email directly, view it on GitHub https://gist.github.com/2be6c114e80223d0e7398c635b489819#gistcomment-4279551, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACG3S2KB343ZSHWQ6XUU6BTV26EUDANCNFSM5YRYF7SQ . You are receiving this because you were mentioned.Message ID: @.***>

Yes, the fopen is open the file succesfull. I try to send a request from postman, and no problem there..
Generate the PHP code with postman, and that doesn't work either:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.clickup.com/api/v2/task/3c7dey2/attachment',
  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('attachment'=> new CURLFILE('/C:/Users/rajna/Downloads/pngwing.com (1).png'),'filename' => 'test.png'),
  CURLOPT_HTTPHEADER => array(
    'Authorization: KEY',
    'Content-Type: multipart/form-data; boundary=12121'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

@tamara-m
Copy link

tamara-m commented Aug 26, 2022

@rajnaviktor

It should not be possible in theory for something to work in postman and not when running from the code, if they are in fact the same, so something has to be up here. It's hard to debug this way, but as a shot in the dark

1/ Enable logging and check if any errors get recorded?
2/ Why is the file name in the path pngwing.com (1).png and then in the filename param it is test.png
3/ Test with a file that does not have a space nor special characters in the name, just to be safe.
4/ Test with this line $file = curl_file_create($file_path, $file_type, $file_name ); instead of your version, and note that this one includes the file type which in your case should be image/png if I am not mistaken
5/ Remove the boundary=12121 from the header in case it is causing issues.

@DmitrySidorenkoShim
Copy link

4/ Test with this line $file = curl_file_create($file_path, $file_type, $file_name ); instead of your version, and note that this one includes the file type which in your case should be image/png if I am not mistaken

thank you
at least I have it working with curl right now

but maybe someone made it working with GuzzleHttp?
I am using:

        $data['multipart'] = [
            [
                'name'     => 'attachment',
                'contents' => Utils::tryFopen(storage_path('app/public/image.png'), 'r'),
            ]
        ];

and i tried fopen() and file_get_content() for 'contents'

but it always:
'{"err":"No attachment supplied","ECODE":"ATTCH_039"}'

I am using Guzzle for all other requests
so, please, let me know with an example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment