Curl file uploads from local - can't get to work in Postman

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?

Thanks, Mike

It is hard to know what is going on based on the limited detailed provided.

A 500 error code may indicate a poorly written application, especially if there is no clear error message that allows you to debug your request.

Since your goal is to use RestShart, try getting the request to work from Postman, not from cURL.

1 Like

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).

Thanks, Mike