Upload zip file with Attachment service-now.com/api/now/attachment/file not working.

cesary
Kilo Contributor

I have a issue with upload zip file. The API Attachement /api/now/attachment/file uploaded the file to ServiceNow. After that, the content-type of this file appear like this example:

"multipart/form-data; boundary=---------------------8d5fbb014e709f7".

The problem, when I downloaded the file, I can´t open the ZIP file. Someone has the same issue or know how to resolve this problem?

 

1 ACCEPTED SOLUTION

terrywang
ServiceNow Employee
ServiceNow Employee

Hi Cesary,

There is a Youtube video on the doc site, which is handy.

https://docs.servicenow.com/bundle/london-application-development/page/integrate/inbound-rest/concept/c_AttachmentAPI.html

 

Two points:

1> Use Invoke-WebRequest

2> Specify content type: 'Content-Type','application/zip' 

 

Below is the script that works in my lab:

 

$FilePath = "c:\asdf\a.zip"

# Eg. User name="admin", Password="admin" for this code sample.
$user = "dummy"
$pass = "asdfasdfasdfasdfasdf"

# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))

# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
$headers.Add('Content-Type','application/zip')

 

# Specify endpoint uri
$uri = "https://emptwangjakarta.service-now.com/api/now/attachment/file?table_name=incident&table_sys_id=d63fb610db5697004fa76f048a9619a3&file_name=myattachment.zip"

# Specify HTTP method
$method = "post"


#{request.body ? "$body = \"" :""}}

# Send HTTP request
$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -InFile $FilePath

# Print response
$response.RawContent

 

===

If this resolves your question, please mark as answered.

Let me know if you have any further question.

Kind Regards,

Terry

View solution in original post

6 REPLIES 6

sachin_namjoshi
Kilo Patron
Kilo Patron

Check this it may helps you.

 

javascript - Unzipping files - Stack Overflow

 

Regards,

Sachin

I´m using the Activity Designer, Workflow´s feature. The Execution template is Powershell.

Here´s my execution command.

"
$FileName = "example.zip"
$FilePath = "D:\_zip_repository\$FileName"

$endpoint = "$SNUrl/api/now/attachment/file?table_name=$Table&table_sys_id=$SysID&file_name=$FileName"

$username= 'admin'
$password = 'XXX'

$webClient = New-Object System.Net.WebClient;
$webClient.Credentials = New-Object System.Net.NetworkCredential($username, $password);
$webClient.UploadFile($endpoint, "POST", $FilePath) | Out-Null

"

The issue when I download this file was corrupted. 

 

Does it work if you do it through something like Postman? Just to narrow it down to being a general SN/API issue or just when using powershell

simonbergstedt
Tera Guru

What content type do you have in the header?