I'm calling the ServiceNow Attachment API to upload files but they are ending up corrupted even though they upload successfully.
ServiceNow Attachment API
POST https://sn-host.com/api/now/attachment/file?table_name=YOURTABLE&table_sys_id=SYSID&file_name=FILENAME
A simple text file with content "test" is corrupted and the content looks like this:
-----------3056BD41-FD91-4B8C-8BCD-E35A47CAA7A3
Content-Disposition: form-data; name="file"; filename="b.txt"
Content-Type: application/x-gzip
test
-----------3056BD41-FD91-4B8C-8BCD-E35A47CAA7A3--
I'm using C# / RestSharp to call the upload api:
private static bool UploadFileBytes(byte[] file_bytes, string file_name, string table_name, string table_sys_id, string content_type)
{
bool retval = false;
try
{
string URL = SN_Srv_dest + $"/api/now/attachment/file?table_name={table_name}&table_sys_id={table_sys_id}&file_name={file_name}";
Console.WriteLine("Post file to SN: " + URL);
var client = new RestClient(URL);
var request = new RestRequest(Method.POST);
request.AddFileBytes("file", file_bytes, file_name);
request.AddHeader("Content-Type", content_type);
client.Authenticator = new HttpBasicAuthenticator(SN_Id_dest, SN_Pw_dest);
IRestResponse response = client.Execute(request);
if (response.IsSuccessful)
{
Console.WriteLine($"Post file {file_name} success: {response.Content}");
retval = true;
}
else
{
Console.WriteLine($"Post file {file_name} failed: {response.Content}");
retval = false;
}
}
catch(Exception e)
{
Console.WriteLine($"Post file {file_name} failed: {e.Message}");
}
return retval;
}
I found the issue using the generated code in PostMan. The following line was causing the problem:
I replaced it with this: