I'm trying to attach a file to a task in Asana through the HttpClient 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: https://asana.com/developers/documentation/getting-started/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.Accept.Clear();
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("https://app.asana.com/api/1.0/tasks/<TASK_ID>/attachments", form);
var input = await response.Content.ReadAsStringAsync();
Console.WriteLine(input);
}
Anyone can help?
found a solution
public async Task<string> AttachFile(string file, string fileName, string id)
{
var decoded = Convert.FromBase64String(file);
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Bearer " + ApiKey);
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data");
MultipartFormDataContent form = new MultipartFormDataContent();
var fileCont = new ByteArrayContent(decoded, 0, decoded.Length);
fileCont.Headers.Add("Content-Disposition", $"form-data; name=\"file\"; filename=\"{fileName}\"");
form.Add(fileCont);
HttpResponseMessage response = await httpClient.PostAsync($@"https://app.asana.com/api/1.0/tasks/{id}/attachments", form);
var responseData = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
return "file was uploaded successfully";
throw new ArgumentException($"Error code: {response.StatusCode}; {responseData}");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.