Using Requests Python Library with ServiceNow JSON V2 Web Service

30 Dec 2013

Using [Requests] 1 Python library with [ServiceNow JSONv2 Web Service] 2

I've been working with the Python Requests library recently to interact with ServiceNow Web Services and found it to be an extremely easy way to get up and running quickly making requests and interacting with ServiceNow's API. In this post I'll run through some sample requests against one of the ServiceNow demo instnaces. These instances are open to the public and often zbooted so be aware that they're configuration may vary. Be aware that the JSONv2 Web Service is only available in the Dublin release of ServiceNow.

##Get an Incident

To get and individual incident you will need to make a GET request passing the sys_id of an individual incident as in the sample below. Note that this is the default action so it is unnecessary to pass the sysparm_action url parameter for this request.

###Code

<script src="https://gist.github.com/8139495.js">

###Request GET https://demo006.service-now.com/incident.do?JSONv2&sysparm\_sys\_id=d71f7935c0a8016700802b64c67c11c6 HTTP/1.1 Host: demo006.service-now.com Authorization: Basic YWRtaW46YWRtaW4= Accept-Encoding: gzip, deflate, compress Accept: / User-Agent: python-requests/2.0.1 CPython/2.7.2

###Response HTTP/1.1 200 OK Content-Type: application/json;charset=utf-8 Date: Thu, 26 Dec 2013 20:10:02 GMT Server: ServiceNow Content-Length: 1809 { "records" :[ { "__status" : "success", "active" : "true", "activity_due" : "2013-12-26 21:23:21", "approval" : "not requested", "approval_history" : "", "approval_set" : "", "assigned_to" : "681b365ec0a80164000fb0b05854a0cd", "assignment_group" : "d625dccec0a8016700a222a0f7900d06", "business_duration" : "", "business_stc" : "", "calendar_duration" : "", "calendar_stc" : "", "caller_id" : "46c1293aa9fe1981000dc753e75ebeee", "category" : "software", "caused_by" : "", "child_incidents" : "", "close_code" : "", "close_notes" : "", "closed_at" : "", "closed_by" : "", "cmdb_ci" : "26e494480a0a0bb400ad175538708ad9", "comments" : "", "comments_and_work_notes" : "", "company" : "31bea3d53790200044e0bfc8bcbe5dec", "contact_type" : "phone", "correlation_display" : "", "correlation_id" : "", "delivery_plan" : "", "delivery_task" : "", "description" : "", "due_date" : "", "escalation" : "3", "expected_start" : "", "follow_up" : "", "group_list" : "", "impact" : "1", "incident_state" : "2", "knowledge" : "false", "location" : "", "made_sla" : "true", "notify" : "1", "number" : "INC0000055", "opened_at" : "2013-12-24 04:47:23", "opened_by" : "681b365ec0a80164000fb0b05854a0cd", "order" : "", "parent" : "", "parent_incident" : "", "priority" : "1", "problem_id" : "d7296d02c0a801670085e737da016e70", "reassignment_count" : "0", "reopen_count" : "", "resolved_at" : "", "resolved_by" : "", "rfc" : "", "severity" : "3", "short_description" : "SAP Sales app is not accessible", "sla_due" : "2013-12-23 12:47:23", "state" : "2", "subcategory" : "", "sys_class_name" : "incident", "sys_created_by" : "itil", "sys_created_on" : "2013-12-24 04:49:39", "sys_domain" : "global", "sys_id" : "d71f7935c0a8016700802b64c67c11c6", "sys_mod_count" : "7", "sys_updated_by" : "system", "sys_updated_on" : "2013-12-26 19:23:22", "time_worked" : "", "u_vip" : "false", "upon_approval" : "proceed", "upon_reject" : "cancel", "urgency" : "1", "user_input" : "", "watch_list" : "", "work_end" : "", "work_notes" : "", "work_notes_list" : "", "work_start" : "" } ]}

Get multiple Incidents

###Code

<script src="https://gist.github.com/8139939.js">

###Request GET https://demo006.service-now.com/incident.do?JSONv2&sysparm\_action=getRecords&sysparm\_query=priority=1%5Estate=1%5Eassignment\_group=8a4dde73c6112278017a6a4baf547aa7 HTTP/1.1 Host: demo006.service-now.com Authorization: Basic YWRtaW46YWRtaW4= Content-Type: application/json Accept-Encoding: gzip, deflate, compress accept: application/json;charset=utf-8 User-Agent: python-requests/2.0.1 CPython/2.7.2 Windows/7

###Response HTTP/1.1 200 OK Content-Type: application/json;charset=utf-8 Date: Mon, 30 Dec 2013 15:16:32 GMT Server: ServiceNow Content-Length: 5554 {"records":[ { "upon_approval":"proceed", "location":"108752c8c611227501d4ab0e392ba97f", "expected_start":"", "reopen_count":"", "close_notes":"", "u_cust_inf":"", "impact":"1", "urgency":"1", "correlation_id":"", "sys_domain":"global", "description":"", "group_list":"", "priority":"1", "delivery_plan":"", "sys_mod_count":"4", "work_notes_list":"", "follow_up":"", "closed_at":"", "u_reason":"", "sla_due":"2013-12-27 12:36:33", "delivery_task":"", "sys_updated_on":"2013-12-27 20:48:32", "parent":"", "work_end":"", "number":"INC0000051", "closed_by":"", "work_start":"", "calendar_stc":"", "business_duration":"", "category":"software", "incident_state":"1", "activity_due":"2013-12-27 22:48:32", "correlation_display":"", "company":"", "active":"true", "due_date":"", "assignment_group":"8a4dde73c6112278017a6a4baf547aa7", "caller_id":"681ccaf9c0a8016400b98a06818d57c7", "knowledge":"false", "made_sla":"true", "comments_and_work_notes":"", "parent_incident":"", "state":"1", "user_input":"", "sys_created_on":"2013-12-27 20:48:32", "approval_set":"", "reassignment_count":"0", "u_test":"", "rfc":"", "child_incidents":"", "opened_at":"2013-12-27 20:48:32", "short_description":"User can't access SAP Controlling application", "order":"", "sys_updated_by":"glide.maint", "resolved_by":"", "notify":"1", "upon_reject":"cancel", "approval_history":"", "__status":"success", "problem_id":"d7296d02c0a801670085e737da016e70", "work_notes":"", "calendar_duration":"", "close_code":"", "sys_id":"d7158da0c0a8016700eef46c8d1f3661", "approval":"not requested", "caused_by":"", "severity":"3", "sys_created_by":"itil", "resolved_at":"", "assigned_to":"9ee1b13dc6112271007f9d0efdb69cd0", "business_stc":"", "cmdb_ci":"26e46e5b0a0a0bb4005d1146846c429c", "opened_by":"681b365ec0a80164000fb0b05854a0cd", "subcategory":"", "sys_class_name":"incident", "watch_list":"", "time_worked":"", "contact_type":"phone", "escalation":"3", "comments":"" }, { "upon_approval":"proceed", "location":"db96a8480a0a0a65019da337d4a89edc", "expected_start":"", "reopen_count":"", "close_notes":"", "u_cust_inf":"", "impact":"1", "urgency":"1", "correlation_id":"", "sys_domain":"global", "description":"", "group_list":"", "priority":"1", "delivery_plan":"", "sys_mod_count":"3", "work_notes_list":"", "follow_up":"", "closed_at":"", "u_reason":"", "sla_due":"2013-12-27 12:40:39", "delivery_task":"", "sys_updated_on":"2013-12-27 20:48:40", "parent":"", "work_end":"", "number":"INC0000052", "closed_by":"", "work_start":"", "calendar_stc":"", "business_duration":"", "category":"software", "incident_state":"1", "activity_due":"2013-12-27 22:48:40", "correlation_display":"", "company":"", "active":"true", "due_date":"", "assignment_group":"8a4dde73c6112278017a6a4baf547aa7", "caller_id":"46c6f9efa9fe198101ddf5eed9adf6e7", "knowledge":"false", "made_sla":"true", "comments_and_work_notes":"", "parent_incident":"", "state":"1", "user_input":"", "sys_created_on":"2013-12-27 20:48:40", "approval_set":"", "reassignment_count":"0", "u_test":"", "rfc":"", "child_incidents":"", "opened_at":"2013-12-27 20:48:40", "short_description":"SAP Financial Accounting application appears to be down", "order":"", "sys_updated_by":"glide.maint", "resolved_by":"", "notify":"1", "upon_reject":"cancel", "approval_history":"", "__status":"success", "problem_id":"d7296d02c0a801670085e737da016e70", "work_notes":"", "calendar_duration":"", "close_code":"", "sys_id":"d7195138c0a8016700fd68449cfcd484", "approval":"not requested", "caused_by":"", "severity":"3", "sys_created_by":"itil", "resolved_at":"", "assigned_to":"46d44a23a9fe19810012d100cca80666", "business_stc":"", "cmdb_ci":"26e426be0a0a0bb40046890d90059eaa", "opened_by":"681b365ec0a80164000fb0b05854a0cd", "subcategory":"", "sys_class_name":"incident", "watch_list":"", "time_worked":"", "contact_type":"phone", "escalation":"3", "comments":"" }, { "upon_approval":"proceed", "location":"db96a8480a0a0a65019da337d4a89edc", "expected_start":"", "reopen_count":"", "close_notes":"", "u_cust_inf":"", "impact":"1", "urgency":"1", "correlation_id":"", "sys_domain":"global", "description":"", "group_list":"", "priority":"1", "delivery_plan":"", "sys_mod_count":"2", "work_notes_list":"", "follow_up":"", "closed_at":"", "u_reason":"", "sla_due":"2013-12-27 12:42:45", "delivery_task":"", "sys_updated_on":"2013-12-27 20:48:46", "parent":"", "work_end":"", "number":"INC0000053", "closed_by":"", "work_start":"", "calendar_stc":"", "business_duration":"", "category":"software", "incident_state":"1", "activity_due":"2013-12-27 22:48:46", "correlation_display":"", "company":"", "active":"true", "due_date":"", "assignment_group":"8a4dde73c6112278017a6a4baf547aa7", "caller_id":"46d4a69ba9fe1981001af9616bf01185", "knowledge":"false", "made_sla":"true", "comments_and_work_notes":"", "parent_incident":"", "state":"1", "user_input":"", "sys_created_on":"2013-12-27 20:48:46", "approval_set":"", "reassignment_count":"0", "u_test":"", "rfc":"", "child_incidents":"", "opened_at":"2013-12-27 20:48:46", "short_description":"The SAP Human Resources application is not accessible", "order":"", "sys_updated_by":"glide.maint", "resolved_by":"", "notify":"1", "upon_reject":"cancel", "approval_history":"", "__status":"success", "problem_id":"d7296d02c0a801670085e737da016e70", "work_notes":"", "calendar_duration":"", "close_code":"", "sys_id":"d71b3b41c0a8016700a8ef040791e72a", "approval":"not requested", "caused_by":"", "severity":"3", "sys_created_by":"itil", "resolved_at":"", "assigned_to":"5137153cc611227c000bbd1bd8cd2005", "business_stc":"", "cmdb_ci":"26e51a2f0a0a0bb4008628d2254c42db", "opened_by":"681b365ec0a80164000fb0b05854a0cd", "subcategory":"", "sys_class_name":"incident", "watch_list":"", "time_worked":"", "contact_type":"phone", "escalation":"3", "comments":"" }] }

Create an Incident

###Code

<script src="https://gist.github.com/8198767.js">

###Request POST https://demo006.service-now.com/incident.do?JSONv2 HTTP/1.1 Host: demo006.service-now.com Content-Length: 183 Accept-Encoding: gzip, deflate, compress Accept: application/json;charset=utf-8 User-Agent: python-requests/2.0.1 CPython/2.7.2 Windows/7 Content-Type: application/json Authorization: Basic YWRtaW46YWRtaW4=

	{
	  "category": "software",
	  "impact": "1",
	  "caller_id": "Abel Tuter",
	  "sysparm_action": "insert",
	  "urgency": "1",
	  "short_description": "new incident from JSONv2 API",
	  "cmdb_ci": "Email"
	}

###Response HTTP/1.1 200 OK Content-Type: application/json;charset=utf-8 Date: Tue, 31 Dec 2013 15:49:11 GMT Server: ServiceNow Content-Length: 1781

{
  "records": [
	{
	  "upon_approval": "proceed",
	  "location": "",
	  "expected_start": "",
	  "reopen_count": "0",
	  "close_notes": "",
	  "service_offering": "",
	  "impact": "1",
	  "urgency": "1",
	  "correlation_id": "",
	  "sys_domain": "global                          ",
	  "description": "",
	  "group_list": "",
	  "priority": "1",
	  "delivery_plan": "",
	  "sys_mod_count": "0",
	  "work_notes_list": "",
	  "follow_up": "",
	  "closed_at": "",
	  "sla_due": "",
	  "delivery_task": "",
	  "sys_updated_on": "2013-12-31 15:49:11",
	  "parent": "",
	  "work_end": "",
	  "number": "INC0010018",
	  "closed_by": "",
	  "work_start": "",
	  "calendar_stc": "",
	  "business_duration": "",
	  "category": "software",
	  "incident_state": "2",
	  "activity_due": "",
	  "correlation_display": "",
	  "company": "227cdfb03710200044e0bfc8bcbe5d6b",
	  "active": "true",
	  "due_date": "",
	  "assignment_group": "8a4dde73c6112278017a6a4baf547aa7",
	  "caller_id": "62826bf03710200044e0bfc8bcbe5df1",
	  "knowledge": "false",
	  "made_sla": "true",
	  "comments_and_work_notes": "",
	  "parent_incident": "",
	  "state": "2",
	  "user_input": "",
	  "sys_created_on": "2013-12-31 15:49:11",
	  "approval_set": "",
	  "reassignment_count": "0",
	  "rfc": "",
	  "child_incidents": "0",
	  "opened_at": "2013-12-31 15:49:11",
	  "short_description": "new incident from JSONv2 API",
	  "order": "",
	  "sys_updated_by": "admin",
	  "resolved_by": "",
	  "notify": "1",
	  "upon_reject": "cancel",
	  "approval_history": "",
	  "__status": "success",
	  "problem_id": "",
	  "work_notes": "",
	  "calendar_duration": "",
	  "close_code": "",
	  "sys_id": "c82e1a607802d100b8cca9890ec54c45",
	  "approval": "not requested",
	  "caused_by": "",
	  "severity": "3",
	  "sys_created_by": "admin",
	  "resolved_at": "",
	  "assigned_to": "6816f79cc0a8016401c5a33be04be441",
	  "business_stc": "",
	  "cmdb_ci": "27d32778c0a8000b00db970eeaa60f16",
	  "opened_by": "6816f79cc0a8016401c5a33be04be441",
	  "subcategory": "",
	  "sys_class_name": "incident",
	  "watch_list": "",
	  "time_worked": "",
	  "contact_type": "phone",
	  "escalation": "0",
	  "u_abc": "",
	  "comments": ""
	}
  ]
}

Update an Incident

###Code

<script src="https://gist.github.com/8222030.js">

###Request POST https://demo006.service-now.com/incident.do?JSONv2&sysparm_query=sys_id=471d4732a9fe198100affbf655e59172 HTTP/1.1 Host: demo006.service-now.com Content-Length: 114 Accept-Encoding: gzip, deflate, compress Accept: application/json;charset=utf-8 User-Agent: python-requests/2.0.1 CPython/2.7.2 Windows/7 Content-Type: application/json Authorization: Basic YWRtaW46YWRtaW4=

{
  "short_description": "incident updated via JSONv2 API at 2014-01-02 10:44:06.100000",
  "sysparm_action": "update"
}

###Response HTTP/1.1 200 OK Content-Type: application/json;charset=utf-8 Date: Thu, 02 Jan 2014 16:44:09 GMT Server: ServiceNow Content-Length: 1740

{
  "records": [
	{
	  "upon_approval": "",
	  "location": "108486c7c611227500b093211aa88dcc",
	  "expected_start": "",
	  "reopen_count": "",
	  "close_notes": "",
	  "impact": "2",
	  "urgency": "2",
	  "correlation_id": "",
	  "sys_domain": "global",
	  "description": "",
	  "group_list": "",
	  "priority": "3",
	  "delivery_plan": "",
	  "sys_mod_count": "9",
	  "work_notes_list": "",
	  "follow_up": "",
	  "closed_at": "",
	  "sla_due": "",
	  "delivery_task": "",
	  "sys_updated_on": "2014-01-02 16:44:09",
	  "parent": "",
	  "work_end": "",
	  "number": "INC0000040",
	  "closed_by": "",
	  "work_start": "",
	  "calendar_stc": "",
	  "business_duration": "",
	  "category": "inquiry",
	  "incident_state": "2",
	  "activity_due": "",
	  "correlation_display": "",
	  "company": "31bea3d53790200044e0bfc8bcbe5dec",
	  "active": "true",
	  "due_date": "",
	  "assignment_group": "",
	  "caller_id": "46c6f9efa9fe198101ddf5eed9adf6e7",
	  "knowledge": "false",
	  "made_sla": "false",
	  "comments_and_work_notes": "",
	  "parent_incident": "",
	  "state": "2",
	  "user_input": "",
	  "sys_created_on": "2013-10-23 00:43:54",
	  "approval_set": "",
	  "reassignment_count": "1",
	  "rfc": "",
	  "child_incidents": "",
	  "opened_at": "2013-10-23 00:42:45",
	  "short_description": "incident updated via JSONv2 API at 2014-01-02 10:44:06.100000",
	  "order": "",
	  "sys_updated_by": "admin",
	  "resolved_by": "",
	  "notify": "1",
	  "upon_reject": "",
	  "approval_history": "",
	  "__status": "success",
	  "problem_id": "",
	  "work_notes": "",
	  "calendar_duration": "",
	  "close_code": "",
	  "sys_id": "471d4732a9fe198100affbf655e59172",
	  "approval": "not requested",
	  "caused_by": "",
	  "severity": "3",
	  "sys_created_by": "admin",
	  "resolved_at": "",
	  "assigned_to": "681b365ec0a80164000fb0b05854a0cd",
	  "business_stc": "",
	  "cmdb_ci": "0c43b896c6112275019abd2b2b93f464",
	  "opened_by": "6816f79cc0a8016401c5a33be04be441",
	  "subcategory": "",
	  "sys_class_name": "incident",
	  "watch_list": "",
	  "time_worked": "",
	  "contact_type": "phone",
	  "escalation": "0",
	  "comments": ""
	}
  ]
}