How to attach a local file using REST API service.My requirement is to create an Incident with an attachment using Python language. I am able to create Incident but not able to attach a .csv file to it.

Rajendra Pasupu
Kilo Explorer

How to attach a local file using REST API service.My requirement is to create an Incident with an attachment using Python language. I am able to create Incident but not able to attach a .csv file to it. Code is below. 

import requests
import json
import pandas as pd
# Set the request parameters

df_delta_file = pd.read_csv('Delta.csv')
number_of_rows = len(df_delta_file.index)

if number_of_rows>=1:
url = 'https://dev.service-now.com/api/now/table/incident?sysparm_display_value=Starbucks%20POC%20Incident'

# Eg. User name="admin", Password="admin" for this code sample.
user = 'admin'
pwd = ''

# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}

# Do the HTTP request
response = requests.post(url, auth=(user, pwd), headers=headers ,data="{\"short_description\":\" TWO\"}")

# 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()

# Decode the JSON response into a dictionary and use the data
data = response.json()
#print(data)
data = response.json()
Final_Data = json.loads(json.dumps(data))
sys_id = Final_Data['result']['sys_id']
#print(Final_Data['result']['sys_id'])

#Need to install requests package for python

# Set the request parameters
curl "https://dev.service-now.com/api/now/attachment/file?table_name=incident&table_sys_id=8d5d2a28db422010f59b560868961967&file_name=Delta.csv" \
--request POST \
--header "Accept:application/json" \
--user 'admin':'
--header "Content-Type: file/csv" \
--data-binary "@<location of the file on file system>"

1 REPLY 1

Pranesh072
Mega Sage
Mega Sage

have you updated the file path in this?

--data-binary "@<location of the file on file system>"