How to upload a file to a task in ASANA api using c#

I’m trying to attach a file to a task in Asana through the C# and I’m getting an error:

{"errors":[{"message":"file: Missing input","help":"For more information on API status codes and how to handle them, read the docs on errors:"}]}

The request I’m making has the following format.

static async void GoPost(byte[] image)
        string ApiKey = "<API_KEY>";

        HttpClient httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Bearer " + ApiKey);
        MultipartFormDataContent form = new MultipartFormDataContent("Upload----");

        form.Add(new ByteArrayContent(image, 0, image.Length), "profile_pic", "1.png");
        HttpResponseMessage response = await httpClient.PostAsync("<TASK_ID>/attachments", form);

        var input = await response.Content.ReadAsStringAsync();

Anyone can help?

Hey @Zahid_Abbasli,

I played around with it, and was able to get it working by comparing it to our Java client library, temporarily adding HttpClient logging, and comparing it to what’s expected in our attachment docs

The ‘name’ is expected to be the word “file”, including the quotes. Just change this line and you should be good to go.

form.Add(new ByteArrayContent(bytes, 0, bytes.Length), "\"file\"", "\"example.jpg\"");

You may also want to change this line to use a random string to ensure no file splitting occurs.

MultipartFormDataContent form = new MultipartFormDataContent(Guid.NewGuid().ToString());

Hope this helps!


Thank you so much,