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

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

cesary
Kilo Contributor

Terry, 

It´s work very well. Thank you so much.

 Best Regards