- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 11-10-2021 05:34 PM
Statement : POST/Upload a file from mid-server to ServiceNow Instance.
Use Case: Many time there are requirements to process files/data securely from mid-server into servicenow instance. E.g. encrypted user information from other platforms such as workday.
There are multiple options to process external files into ServiceNow but I found attachment API approach is more efficient in terms of processing & security.
Other option to python: PowerShell, ShellScript, cURL
import requests, sys, os
import pprint
import json, base64
try:
#If input data is encoded.
#input_data = json.loads(base64.b64decode(sys.argv[1]).decode('ascii'))
endpoint = "https://<host>.service-now.com/api/now/attachment/upload"
username = "username"
password = "password"
filename = 'ExampleFile.csv' // File Name with extension
filepath = 'ExampleDir/ExampleFile.csv'
//FilePath defaulted to midserver/agent as script run in current folder.
filetype = 'csv' // csv, xlxs, pdf, doc
tablename = "sys_data_source". // Target ServiceNow table name
tablesysid = 'sys_id' //Record SysId where attachment needs to be posted.
# Set the request parameters
url = endpoint
# Open & Read file from location
files = {"file": (filename, open(filepath, "rb"), filetype)}
# Set proper headers
headers = {"Accept":"*/*"}
# Request Payload
payload = {"table_name": tablename,"table_sys_id": tablesysid}
# Do the HTTP request
response = requests.post(url, auth=(username, password),data=payload, headers=headers, files=files)
#print(response.json())
# Check for HTTP codes other than 201
if response.status_code == 201:
#print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
output['status_code']=response.status_code
output['status']="success"
output['response']=response.json()
else:
#print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
output['status_code']=response.status_code
output['status']="failed"
output['response']=response.json()
// Error handler - can be modified as per need.
except ValueError:
#print("Error"+ValueError.with_traceback),
output['status_code']=401
output['status']="failed"
output['response']=ValueError.with_traceback
Simple way to call python script in IntegrationHub
- PowerShell Step -- If Windows hosted mid-server
- SSH -- if Linux hosted mid-server
Argument to python script is optional. It depends how you want to handle inputs to the flow action.
Soon will post another article on how to copy PGP encrypted files from SFTP to mid-server and decrypt those files using python.
- 2,626 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi - Just checking if you have any updates on the use case "PGP encrypted files from SFTP to mid-server and decrypt those files using python."