API php attachement

attachments

#1

Hi,

I start to use Asana API without the wrapper.
I manage to connect to the API, get user, tags, create a task.

But I cannot add an attachment to a task. I found a lot of subject on the Internet but the way to send the file stay ambigous and i found no solution.

I don’t understand how send my image :
$body[‘file’] = ‘@’.file_get_contents(‘50026.jpg’);
$body[‘file’] .= ‘;filename=test.jpg’;
$body[‘file’] .= ‘;type=image/jpg’;

The return of the call is :

Content-Length: 145
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------871569ea1ed194c9

)

Could you share some php code to help me ?


#2

Hi,

I suppose you have seen the official documentation about attachments? I never used this feature so I can’t help.

Bastien


#3

Hi,

Yes i watch the Asana documentation.
The way to send document is strange and I never meet the same thing on other APis.


#4

hey. I hope I am not late for the solution :stuck_out_tongue:

$path = ‘images/image.jpeg’;
$type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
$asanaFilename = ‘image.jpeg’;

$client->attachments->createOnTask(
$task->id,
file_get_contents($path),
$asanaFilename,
$type
);


#5

Hi !

You seem use the wrapper. I don’t use it.
But, it give me many informations that i will try.
Thank you.


#6

Hi @Stephane,

File attachments are quite odd at first glance - they don’t seem similar to any other part of our API because they are a multi-part upload. This is in fact standard across most APIs (I arbitrarily found Google Drive’s docs on multipart uploads as an example), but it’s possible that you might have been isolated in the past from the details of how this is done using various client-side tools - for instance, cURL makes it pretty easy (as in our examples), but cURL is doing the same thing for you that you can do manually for any multipart upload.

I’ll draw your attention to the example shown in our docs, snipping out the important parts:

HTTP headers:

Content-Type: multipart/form-data; boundary=<UNIQUE_BOUNDARY>
Content-Length: 141

Body of the request:

--<UNIQUE_BOUNDARY>
Content-Disposition: form-data; name="file"; filename="example.txt"
Content-Type: text/plain

<RAW_FILE_DATA>

--<UNIQUE_BOUNDARY>--

So you set HTTP headers to have Content-Type: multipart/form-data; boundary=<UNIQUE_BOUNDARY> and the size of the request content with Content-Length: <size_in_bytes>

Then (this is where things can get confusing) in the body of the request, start with -- and the boundary you specified in the HTTP header and a CR/LF carriage return.

Then there are header-like entries that you include for the metadata of each file. These are not HTTP headers, but are in the body of the request itself. They tell the server information about the file you’re sending.

Between these headers and the start of the file are two CR/LF carriage returns.

Next comes the bytes that constitute the file itself, then a CR/LF carriage return.

Finally, there’s --, the boundary again, and --.

All of this is, confusingly, a pretty standard way to support the ability to upload files to a server. If you have experienced file uploads that behave differently, I’d wager that you were using a tool or library that abstracted those nitty-gritty details into a friendlier interface.

To that end, I think what your PHP code looks like will largely depend on what tools you’re using to do the upload - if it’s our client library, I believe this basic logic starts to be handled here

Sorry this is confusing, and good luck!


#7

Hi,

Thanks for your help. With the wrapper, all is ok for me.