The CreatorCon Call for Content is officially open! Get started here.

VaranAwesomenow
Mega Sage

############Python script to create incident##############

#Need to install requests package for python

#easy_install requests

import requests

 

# Set the request parameters

url = 'https://devXXXX.service-now.com/api/now/table/incident'

 

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

user = ‘generic’

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\":\"test\"}")

 

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

Python script to read data

Install ServiceNow Python library :  servicenow · PyPI

 pip install servicenow

###############Python script to read data ####################

Endpoint url =

 

#REST API to read ServiceNow data

#Need to install requests package for python

#easy_install requests

import requests

import json

def buildUrl (instanceName, api, table, query, limit):

    url = 'https://'+ instanceName + '.service-now.com' + api + table + "?" + "sysparm_query=" + query + "&sysparm_limit=" + limit

    return url

 

def readServiceNowData(url,username,password):

    # Set proper headers

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

    print (url + " " + username + " " + password)

    # Do the HTTP request

    response = requests.get(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()

    result = {}

    #responseJSON = response.json()['result']

    responseJSON = response.json()['result']

    return responseJSON

# Set the request parameters

#url = 'https://dev63486.service-now.com/api/now/table/incident?sysparm_limit=10'

instanceName = 'dev63486'

api = '/api/now/table/'

table = 'incident'

#query = 'active%3Dtrue%5Estate%3D2'

query = 'active=true^state=2'

limit = '100'

url = buildUrl(instanceName,api,table,query,limit)

#print(url)

 

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

user = 'admin'

pwd = '****'

 

responseJSON = readServiceNowData(url,user,pwd)

for item in responseJSON:

    print(str(item['number']))



 

Comments
Svenom
Kilo Contributor

similarly, trying to create ServiceNow KB article  using ServiceNow Table API 

Currently we're passing html content data (with tags) in ‘text’ field and able to create KB article

[..]

response = requests.post(url, auth=(user, password), headers=headers, data="{\"active\":\"True\",\"short_description\":\"TEST KB Article\",\"text\":\"<h1>Example Knowledge Base Article</h1> <p>Testing contents</p>}")

[..]

 

Instead of passing the html or text content in ‘text’ field, we're trying options to read the entire html file or any file content and create KB article in ServiceNow.

 

Any suggestions or workarounds ?

Thanks,

 

Version history
Last update:
‎03-04-2021 06:25 AM
Updated by: