- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2022 10:00 PM
Hi,
I am a newbie to REST API.
I need to use ServiceNow's Attachment API and PowerShell to upload a file to a field of type "File Attachment" in a certain table.
How do I go about writing the code, etc.?
Table name:x_540069_sn21_systemid
"File Attachment" type fields name:u_nw_kouseizu
target record:9fb8d7badb917010c42a8e4748961977
File name to upload:File Attachment test.xlsx
Can someone please tell me how to do this?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
-
Team Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 03:26 AM
Fixed the script and was able to execute it from my Windows PC to successfully attach a file to a form in my ServiceNow PDI.
# Eg. User name="admin", Password="admin" for this code sample.
$user = "<username>"
$pass = "<password>"
# 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('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
$filePath = "<path to file>/test1.xlsx"
# Specify endpoint uri
$uri = "https://<instance name>.service-now.com/api/now/attachment/file?table_name=<table name>&table_sys_id=<sys_id of record to attach file>&file_name=test1.xlsx"
# Specify HTTP method
$method = "post"
# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -InFile $filePath -Uri $uri
# Print response
$response.RawContent
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2022 10:06 PM
Check below topic for help on this
https://developer.servicenow.com/print_page.do?release=paris&category=null&identifier=c_AttachmentAPI&module=api
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2022 11:01 PM
Already checked, can't see how to specify the field.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2022 11:50 PM
Haven't tested but something like below. Replace <username>,<password>, and <instance name>.
# Eg. User name="admin", Password="admin" for this code sample.
$user = "<username>"
$pass = "<password>"
# 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')
# Specify endpoint uri
$uri = "https://<instance_name>.service-now.com/api/now/attachment/file?table_name=x_540069_sn21_systemid&table_sys_id=9fb8d7badb917010c42a8e4748961977&file_name=test.xlsx"
# Specify HTTP method
$method = "post"
# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri
# Print response
$response.RawContent
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 12:08 AM
It had been confirmed that such code could be created using the REST API Explorer.
However, I am unable to specify a field of type "File Attachment".
Also, when I run this code, I get an error and I have no idea why.