Reputation: 3
I'm trying to upload an attachment to an issue in BIM 360 Field by an application (.NET framework). I'm using following endpoint (BIM 360 Field API Doc: https://bim360field.autodesk.com/apidoc/index.html#mobile_api_method_21).
To build the request I'm using Flurl and MultipartFormDataContent (see code below). However I get a 500 Internal Server Error back and unfortunately there is no more specific information what exactly went wrong.
I tried to upload an attachment with exactly the same url, ticked, project_id and 'attachment detail string' via Postman and it worked fine.
Code:
byte[] fileByteArray = memoryStream.ToArray();
string attachment = JsonConvert.SerializeObject(attachmentDetails);
MultipartFormDataContent conntent = new MultipartFormDataContent();
conntent.Add(new ByteArrayContent(fileByteArray, 0, fileByteArray.Length), "original");
conntent.Add(new StringContent(attachment), "attachment");
FlurlClient client = fieldConnection.GetClient("attachments");
client.Url.SetQueryParam("ticket", Ticket);
client.Url.SetQueryParam("project_id", project);
return await client
.WithTimeout(30)
.SendAsync(HttpMethod.Post, conntent)
.ConfigureAwait(false);
I expect there is a problem with my MultipartFormDataContent. Am I missing something?
Best regards
Chris
Upvotes: 0
Views: 178
Reputation: 7070
It seems to be an issue of the Flurl library (ref) and be always failed to post the right content. Anyway, here is the working code snippet for playing with Flurl. Hope it helps!
BTW,
this API to part of the classical BIM360 Field, and it's not part of the Forge platform. Therefore, please post your questions to https://forums.autodesk.com/t5/bim-360-api-forum/bd-p/115, Thanks!
public static HttpResponseMessage AttachmentsByFlurl(string ticket, string project_id,
string originalPath, string thumbPath,
string container_id, string container_type)
{
// Compose Attachment JSON string
FileInfo original = new FileInfo(originalPath);
Dictionary<string, string> att = new Dictionary<string, string>();
// date time format: "2015-08-05 15:28:17 -0500";
string dateTimeFormat = "yyyy-MM-dd HH:mm:ss zzz";
string curTime = DateTime.Now.ToString(dateTimeFormat);
att["fcreate_date"] = original.CreationTime.ToString(dateTimeFormat);
att["fmod_date"] = original.LastWriteTime.ToString(dateTimeFormat);
att["created_at"] = curTime;
att["updated_at"] = curTime;
att["size"] = original.Length.ToString();
att["content_type"] = MimeMapping.GetMimeMapping(original.Name);
att["filename"] = original.Name;
att["container_id"] = container_id; // e.g., issue_id
att["container_type"] = container_type; // e.g., "Issue"
// Conver to JSON format
string attachment = Newtonsoft.Json.JsonConvert.SerializeObject(att);
var mpc = new MultipartContent();
var ticketContent = new StringContent(ticket);
ticketContent.Headers.Add("Content-Disposition", "form-data; name=\"ticket\"");
mpc.Add(ticketContent);
var projectIdContent = new StringContent(project_id);
projectIdContent.Headers.Add("Content-Disposition", "form-data; name=\"project_id\"");
mpc.Add(projectIdContent);
var attachmentContent = new StringContent(attachment, Encoding.UTF8, "application/json");
attachmentContent.Headers.Add("Content-Disposition", "form-data; name=\"attachment\"");
mpc.Add(attachmentContent);
var attachmentFileStream = File.OpenRead(originalPath);
var attachmentContentStream = new StreamContent(attachmentFileStream);
attachmentContentStream.Headers.Add("Content-Disposition", string.Format("form-data; name=\"original\"; filename=\"{0}\"", Path.GetFileName(originalPath)));
mpc.Add(attachmentContentStream);
if(!string.IsNullOrEmpty(thumbPath))
{
var thumbFileStream = File.OpenRead(thumbPath);
var thumbContentStream = new StreamContent(thumbFileStream);
thumbContentStream.Headers.Add("Content-Disposition", string.Format("form-data; name=\"thumb\"; filename=\"{0}\"", Path.GetFileName(thumbPath)));
mpc.Add(thumbContentStream);
}
var url = "https://bim360field.autodesk.com/api/attachments";
var resp = url
.PostAsync(mpc)
.Result;
return resp;
}
Upvotes: 1