Attachment API getting attachment content showing binary rather than image
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2022 08:56 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 01:55 AM
Following in a python script to download an attachment file.
Note the "Accept":"*/*" in header to accept image file and then writing the response content as a binary file at the end. The error message in the third party application seems like it's trying to accept a String. The error message (The string to be decoded contains characters outside of the Latin1 range" suggest this. It may be trying to accept a response in Base64 encoded format instead of binary.
import requests
# Eg. User name="admin", Password="admin" for this code sample.
user = '<username>'
pwd = '<password>'
instanceName = "<instance name>"
sys_id = "<sys_id of file in sys_attachment table>"
output_filepath = "<full path of output file>"
# Set the request parameters
url = f'https://{instanceName}.service-now.com/api/now/attachment/{sys_id}/file'
# Set proper headers
headers = {"Content-Type":"application/json","Accept":"*/*"}
# Do the HTTP request
response = requests.get(url, auth=(user, pwd), headers=headers )
# Check for HTTP codes other than 200
if response.status_code != 200:
print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
exit()
file = open(output_filepath, "wb")
file.write(response.content)
file.close()

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 01:36 AM
Following is a PowerShell script to download an image file. I'm able to download an image file from ServiceNow to a local pc by running this script.
The problem isn't with ServiceNow API but on how it's being called.
# Eg. User name="admin", Password="admin" for this code sample.
$user = "<username>"
$pass = "<password>"
$instanceName = "<instance name>"
$sys_id = "<sys_id of file in sys_attachment table>"
$output_file = "<full path of output file>"
# 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','*/*')
# Specify endpoint uri
$uri = "https://" + $instanceName + ".service-now.com/api/now/attachment/" + $sys_id + "/file"
# Specify HTTP method
$method = "get"
# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri -OutFile $output_file
# Print response
$response.RawContent

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 02:00 AM
If the third party requires response as Base64 encoded string, create a Scripted API that's get the file from sys_attachment table in Base64 and return the result.
Following thread has a sample code on getting attachment in Base64.