0

I am trying to send an image using Asana's API but it just attaches a blank file. This is the code I have been using.

client.attachments.create_on_task(task_id=123456789,file_content="Url_of_file",file_name='Name_of_File',file_content_type="image/jpeg")

I have tried using different file formats like .txt and .png but for some reason the Asana API is blocking my requests.It just posts a black image on Asana. I have tried to convert the file to base64 as well but it still doesn't work

In this original documentation(below), it shows that we need to pass two arguments;one for the file's content and the other for file itself ('file').

def create_on_task(self, task_id, file_content, file_name, file_content_type=None, **options):
        """Upload an attachment for a task. Accepts a file object or string, file name, and optional file Content-Type"""
        path = '/tasks/%d/attachments' % (task_id)
        return self.client.request('post', path, files=[('file', (file_name, file_content, file_content_type))], **options)

But when I am trying to pass the arguments for file and file content it shows me an error.

Can somebody please help me with this?

busybear
  • 10,194
  • 1
  • 25
  • 42

1 Answers1

0

Another user on the Asana development forum had the same problem using the Curl API. (https://forum.asana.com/t/sending-file-with-api/16897/2). Apparently it has something to do with the 'multipart form upload'.

Looking through another thread here on stackoverflow (How to send a "multipart/form-data" with requests in python?), it seemed the file object had to be read in binary was all.

so the parameters would be:

client.attachments.create_on_task(task_id=<task id here>,file_content=open(filename_with_path, 'rb'),file_name='Name_of_File',file_content_type="image/jpeg")
Jonathan
  • 6,507
  • 5
  • 37
  • 47
tui_w_k
  • 1
  • 2