Samanage has aâŚunique API from any I have seen before. To upload an attachment, the following curl file works in Windows:
curl https://api.samanage.com/attachments.json
-H âX-Samanage-Authorization: Bearer ~TOKEN~â
-F âfile[attachable_type]=Incidentâ
-F âfile[attachable_id]=~incidentid~â
-F âfile[attachment]=@C:/Users/mhagesfeld/Downloads/WHDAPIGuide.pdfâ
-H âContent-Type: multipart/form-dataâ
However, when I save this as a file and import it to Postman, it creates the following, which results in a 500 error:
curl --location --request POST âhttps://api.samanage.com/attachments.jsonâ
âheader âX-Samanage-Authorization: Bearer ~token~â
âheader âContent-Type: multipart/form-dataâ
âform âfile[attachable_type]=Incidentâ
âform âfile[attachable_id]=~incidentid~â
âform âfile[attachment]=@C:/Users/mhagesfeld/Downloads/WHDAPIGuide.pdfâ
The sample curl from the Samanage site is:
curl -H âX-Samanage-Authorization: Bearer TOKENâ
-F âfile[attachable_type]=Incidentâ
-F âfile[attachable_id]=12345678â
-F âfile[attachment]=@/tmp/example.pngâ
-H âAccept: application/vnd.samanage.v1.3+jsonâ
-H âContent-Type: multipart/form-dataâ
-X POST https://api.samanage.com/attachments.json
I replaced TOKEN, incident ID and atachment location and ran this importedâŚstill 500 error.
Has anyone seen anything like this before or have any clue how I might be able to get it into Postman, and then into RestSharp?
I used https://curl.olsh.me/ to get the following that works in C#:
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod(âPOSTâ), âhttps://api.samanage.com/attachments.jsonâ))
{
request.Headers.TryAddWithoutValidation(âX-Samanage-Authorizationâ, âBearer TOKENâ);
var multipartContent = new MultipartFormDataContent();
multipartContent.Add(new StringContent("Incident"), "file[attachable_type]");
multipartContent.Add(new StringContent("56936836"), "file[attachable_id]");
multipartContent.Add(new ByteArrayContent(File.ReadAllBytes("C:/Users/mhagesfeld/Downloads/WHDAPIGuide.pdf")), "file[attachment]", Path.GetFileName("C:/Users/mhagesfeld/Downloads/WHDAPIGuide.pdf"));
request.Content = multipartContent;
var response = await httpClient.SendAsync(request);
}
}
The issue is with having the file contents be part of a parameter, this specific line:
multipartContent.Add(new ByteArrayContent(File.ReadAllBytes(âC:/Users/mhagesfeld/Downloads/WHDAPIGuide.pdfâ)), âfile[attachment]â, Path.GetFileName(âC:/Users/mhagesfeld/Downloads/WHDAPIGuide.pdfâ));
Adding it as a file does not work, and Postman does not seem to allow this type of parameter, unless I am missing something (and I admit I am not fully familiar with the MultiFormDataContent object).