SolarWinds API - Attachment is uploading but not associating to parent

I'm trying to upload an attachment to an existing incident using the API. I can see I'm receiving a 201 HTTP status; however, when I look at the incident on SolarWinds no attachment is showing. The attachment exists though, and I'm able to navigate to it but no association/relationship.

            HttpClient client = new HttpClient();
            HttpRequestMessage message = new HttpRequestMessage();

            FileStream f = File.Open(attachment.AttachmentContent.Path, FileMode.Open);
            
            MultipartFormDataContent form = new MultipartFormDataContent();
            form.Add(new StringContent(attachment.AttachmentContent.Type), "file[attachable_type]");
            form.Add(new StringContent($"file[attachable_id]={Convert.ToInt32(attachment.AttachmentContent.Number)}"));
            form.Add(new StreamContent(f), "file[attachment]", Path.GetFileName(attachment.AttachmentContent.Path));

            message.RequestUri = new Uri(url);
            message.Content = form;
            message.Method = HttpMethod.Post;

            client.DefaultRequestHeaders.Add("X-Samanage-Authorization", "Bearer " + AuthenticationToken);
            HttpResponseMessage response = client.SendAsync(message).Result;
            LastResponse = response;

            if (!response.IsSuccessStatusCode) throw new ApplicationException("Didn't receive OK 200");
            return response.Content.ReadAsStringAsync().Result;

I'm baffled now and out of ideas. I hope someone is able to assist.

Note: I've put this in the wrong forum previously, not a repeat I'll be deleting the other one shortly.

Parents Reply Children
  • Hi Rinat,

    I've managed to resolve the issue. It looks like it was something I did wrong.

    New code attached below.

                HttpClient client = new HttpClient();
                HttpRequestMessage message = new HttpRequestMessage();
    
                FileStream f = File.Open(attachment.AttachmentContent.Path, FileMode.Open);
    
                MultipartFormDataContent form = new MultipartFormDataContent();
                form.Add(new StringContent(attachment.AttachmentContent.Type), "file[attachable_type]");
                form.Add(new StringContent(attachment.AttachmentContent.Number), "file[attachable_id]");
                form.Add(new StreamContent(f), "file[attachment]", attachment.AttachmentContent.Path);
    
                message.RequestUri = new Uri(url);
                message.Content = form;
                message.Method = HttpMethod.Post;
    
                client.DefaultRequestHeaders.Add("X-Samanage-Authorization", "Bearer " + AuthenticationToken);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.samanage.v1.3+json"));
    
                HttpResponseMessage response = client.SendAsync(message).Result;
                LastResponse = response;
    
                if (!response.IsSuccessStatusCode) throw new ApplicationException("Didn't receive OK 200");
                return response.Content.ReadAsStringAsync().Result;