Posting attachment to Rest API to a sys_data_source importset table

Paramahanns
Tera Expert

Hello All,

I am trying to post an attachment to service now URL.

Code in Python

#Need to install requests package for python
#easy_install requests
import requests

# Set the request parameters
url = 'https://dev13093.service-now.com/api/now/attachment/file?table_name=sys_data_source&table_sys_id=091...'

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

files = {'file': ('databaseinstancesample.xls', open('databaseinstancesample.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
# Set proper headers
headers = {"Accept":"*/*"}
#headers = {"Content-Type":"text/csv","Accept":"application/json"}

# Do the HTTP request

response = requests.post(url, auth=(user, pwd), headers=headers, files=files)
#response = requests.post(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()

# Decode the JSON response into a dictionary and use the data
data = response.json()
print(data)

The response after post is

Output of script execution.

Status: 201 Headers: {'Set-Cookie': 'JSESSIONID=0DEF4A0F0D36FF9ED98ABBE51BB0B647

;Secure; Path=/; HttpOnly, glide_user="";Secure; Expires=Thu, 01-Jan-1970 00:00:

10 GMT; Path=/; HttpOnly, glide_user_session="";Secure; Expires=Thu, 01-Jan-1970

00:00:10 GMT; Path=/; HttpOnly, glide_user_route=glide.dc79b2032b94f18b5d0c03f6

493be7e6;Secure; Expires=Tue, 30-Jan-2085 14:23:50 GMT; Path=/; HttpOnly, glide_

session_store=D1BF5BCD4F1432007E65F6E01310C76E;Secure; Expires=Thu, 12-Jan-2017

12:09:43 GMT; Path=/; HttpOnly, BIGipServerpool_dev13093=86415898

6.48958.0000; path=/', 'Content-Encoding': 'gzip', 'Location': 'https://dev13093.service-now.com/api/now/attachment/1dbf138d4f1432007e65f6e01310c

7b7/file', 'Pragma': 'no-store,no-cache', 'Cache-control': 'no-cache,no-store,mu

st-revalidate,max-age=-1', 'Expires': '0', 'Content-Type': 'application/json;cha

rset=UTF-8', 'Transfer-Encoding': 'chunked', 'Date': 'Thu, 12 Jan 2017 11:09:43

GMT', 'Server': 'ServiceNow', 'Strict-Transport-Security': 'max-age=15768000; in

cludeSubDomains;'} Error Response: {'result': {'size_bytes': '35030', 'file_name

': 'databaseinstancesample.xls', 'sys_mod_count': '0', 'average_image_color': ''

, 'image_width': '', 'sys_updated_on': '2017-01-12 11:09:43', 'sys_tags': '', 't

able_name': 'sys_data_source', 'encryption_context': '', 'sys_id': '1dbf138d4f14

32007e65f6e01310c7b7', 'image_height': '', 'sys_updated_by': 'soap.guest', 'down

load_link': 'https://dev13093.service-now.com/api/now/attachment/

1dbf138d4f1432007e65f6e01310c7b7/file', 'content_type': 'multipart/form-data; bo

undary=32f54b4061', 'sys_created_on': '2017-01-12 11:09:43', 'size_compressed':

'10032', 'compressed': 'true', 'table_sys_id': '091fa8b56f7f62006f94c951be3ee469

', 'sys_created_by': 'admin'}}

The file is reaching the instance. But the file is in the corrupted state. Not sure what is the mistake I am doing in the code. Can some one throw some thoughts. Thanks for your replies in advance

Regards

Param

5 REPLIES 5

Paramahanns
Tera Expert

it hits the service now URL but the attachment looks to be corrupted..


Paramahanns
Tera Expert

Can some one give a reason why I am facing this issue.


Ankur Bawiskar
Tera Patron
Tera Patron

Hi Paramahanns,



From where you are trigerring the code to upload the attachment to servicenow record and how are you receiving the attachment?



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thanks for the replies.


I have installed python on a stand alone box. When I execute the python code. The file what specified in the code is posted to the service now url mentioned. But the file in the source is not corrupted. But when I see in service now it is received in a corrupted format. Not sure if there is a mistake in the python code.