Attach a pdf from 3rd party application
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2025 10:42 AM
Good afternooon,
I was hoping someone can shed some light regarding attaching items from a 3rd party app via rest. I'm not sure if I am understanding correctly, but whatever file has to be attach must be "base64 encoded" first? I'm just not sure how the attachments piece work and how to grab an attachment from one application and send into ServiceNow via the attachment API.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2025 11:03 AM
Hi,
Yes, when attaching files from a third-party application to ServiceNow via the Attachment API, the file must be Base64-encoded before sending it in the request body. Here’s how it works:
How Attachments Work in ServiceNow via REST API
- Get the file from the source (third-party app).
- Convert the file to Base64 format.
- Send a POST request to the ServiceNow Attachment API, including the Base64-encoded file in the request body.
- Attach the file to the desired record in ServiceNow (e.g., an incident, task, or custom table record).
Steps to Attach a File via REST API
1. Base64 Encode the File
Before sending the attachment, you need to convert it to a Base64 string.
Python Example (Converting a File to Base64):
import base64
file_path = "example.pdf" # Replace with your file
with open(file_path, "rb") as file:
encoded_string = base64.b64encode(file.read()).decode("utf-8")
print(encoded_string) # Use this in the API request
2. Send the File to ServiceNow Attachment API
Now, use the ServiceNow Attachment API to attach the file to a record.
API Endpoint:
POST https://your-instance.service-now.com/api/now/attachment/file
Headers:
{
"Content-Type": "application/json",
"Authorization": "Basic base64_encoded_credentials"
}
Payload Example:
{
"file_name": "example.pdf",
"table_name": "incident",
"table_sys_id": "abc123xyz456", # Replace with actual record Sys ID
"content_type": "application/pdf",
"data": "BASE64_ENCODED_STRING_HERE"
}
3. Example Using Python Requests
You can automate the file upload using requests in Python:
import requests
import base64
# ServiceNow API Details
instance = "your-instance"
username = "your-username"
password = "your-password"
# Encode file to Base64
file_path = "example.pdf"
with open(file_path, "rb") as file:
encoded_string = base64.b64encode(file.read()).decode("utf-8")
# API Request
url = f"https://{instance}.service-now.com/api/now/attachment/file"
headers = {
"Content-Type": "application/json"
}
payload = {
"file_name": "example.pdf",
"table_name": "incident",
"table_sys_id": "abc123xyz456", # Replace with actual sys_id
"content_type": "application/pdf",
"data": encoded_string
}
response = requests.post(url, auth=(username, password), json=payload, headers=headers)
# Print Response
print(response.json())
Alternative: Send File as Multipart Form Data
Another approach is to send the file as multipart form data instead of Base64.
files = {'file': ('example.pdf', open('example.pdf', 'rb'), 'application/pdf')}
response = requests.post(url, auth=(username, password), files=files)
This method is simpler and avoids the need to encode files manually.
Conclusion
Yes, Base64 encoding is required if sending the file in the request body.
You can alternatively send the file as multipart form data (simpler approach).
Ensure you include the sys_id of the record where the file should be attached.
Use the Attachment API endpoint: /api/now/attachment/file
Kindly mark it as "Accepted Solution"/"helpful", as it resolves your query. Please press like button for the resolution provided.
With Regards,
Krishna Kumar M - Talk with AIT3ch
LinkedIn: https://www.linkedin.com/in/mkrishnak4/
YouTube: https://www.youtube.com/@KrishAIT3CH
Topmate: https://topmate.io/mkrishnak4 [ Connect for 1-1 Session]