REST API Explorer 코드 샘플에서 ServiceNow 인스턴스에 HTTP Request(POST)를 건너뛰어 Incident를 기표한다

12839 단어 ServiceNow
Version: Kingston

이 문서에 따르면 (Version : London)

ServiceNow는 "HTTP 기반 웹 서비스를 통해 다양한 애플리케이션 간의 상호 작용을 가능하게 합니다. 이 기사에서는 REST API를 사용해보고 싶습니다.

만드는 것



bash에서 ServiceNow 인스턴스로 HTTP Request (POST)를 건너 뛰고
[root@cfebb9fa5330 zabbix]# cd /usr/lib/zabbix/alertscripts
[root@cfebb9fa5330 alertscripts]# vi aaa.sh
#!/bin/bash

curl "https://********.service-now.com/api/now/table/incident" \
--request POST \
--header "Accept:application/json" \
--header "Content-Type:application/json" \
--data "{\"short_description\":\"can you explain difference between rice bowl and rice ball\"}" \
--user 'admin':'********'
:wq!
[root@cfebb9fa5330 alertscripts]# chmod 755 aaa.sh
[root@cfebb9fa5330 alertscripts]# ./aaa.sh
{"result":{"parent":"","made_sla":"true","caused_by":"","watch_list":"","upon_reject":"cancel","sys_updated_on":"2018-07-01 06:15:38","child_incidents":"0","hold_reason":"","approval_history":"","skills":"","number":"INC0010131","resolved_by":"","sys_updated_by":"admin","opened_by":{"link":"https://********.service-now.com/api/now/table/sys_user/6816f79cc0a8016401c5a33be04be441","value":"6816f79cc0a8016401c5a33be04be441"},"user_input":"","sys_created_on":"2018-07-01 06:15:38","sys_domain":{"link":"https://********.service-now.com/api/now/table/domain/09ff3d105f231000b12e3572f2b4775d","value":"09ff3d105f231000b12e3572f2b4775d"},"state":"1","task_for":"","sys_created_by":"admin","knowledge":"false","order":"","calendar_stc":"","closed_at":"","u_work_notes":"","cmdb_ci":"","delivery_plan":"","contract":"","impact":"3","active":"true","work_notes_list":"","business_service":"","priority":"5","sys_domain_path":"!!!/!!#/!!!/","rfc":"","time_worked":"","expected_start":"","opened_at":"2018-07-01 06:15:38","business_duration":"","group_list":"","work_end":"","caller_id":"","reopened_time":"","resolved_at":"","approval_set":"","subcategory":"","work_notes":"","short_description":"can you explain difference between rice bowl and rice ball","close_code":"","correlation_display":"","delivery_task":"","work_start":"","assignment_group":"","additional_assignee_list":"","business_stc":"","description":"","calendar_duration":"","close_notes":"","notify":"1","sys_class_name":"incident","closed_by":"","follow_up":"","parent_incident":"","sys_id":"3df6a51d4f031300bac28020a310c7ee","contact_type":"","reopened_by":"","incident_state":"1","urgency":"3","problem_id":"","company":"","reassignment_count":"0","activity_due":"","assigned_to":"","severity":"3","comments":"","approval":"not requested","sla_due":"","comments_and_work_notes":"","due_date":"","sys_mod_count":"0","reopen_count":"0","sys_tags":"","escalation":"0","upon_approval":"proceed","correlation_id":"","location":"","category":"inquiry"}}
[root@cfebb9fa5330 alertscripts]#

Incident를 기표합니다.



REST API Explorer 사용



cURL에서 POST



Filter navigator에 rest를 입력하고 [System Web Services] - [▼REST] - [REST API Explorer]를 표시합니다. tableName에서 Incident를 선택합니다. 정보를 얻고 싶을 때는 GET, 편집하고 싶을 때는 PUT, 삭제하고 싶을 때는 DELETE 등과 HTTP Method를 구분할 수 있습니다.



화면을 아래로 스크롤하고 Request Body에서 Short Description을 선택하고 적절한 메시지를 입력하면 JSON의 Body가 표시됩니다.



[Send]를 클릭해 보겠습니다 201 Created에서 HTTP Request (POST)가 성공한 것 같습니다.



화면을 조금 위로 스크롤하면 코드 샘플이있었습니다.



코드를 복사하여 실행 가능한 환경에서 bash
[root@cfebb9fa5330 zabbix]# cd /usr/lib/zabbix/alertscripts
[root@cfebb9fa5330 alertscripts]# vi aaa.sh

편집하고 붙여 넣습니다 (User ID, Password는 인스턴스 계정의 값에 맞게 편집합니다)
#!/bin/bash

curl "https://********.service-now.com/api/now/table/incident" \
--request POST \
--header "Accept:application/json" \
--header "Content-Type:application/json" \
--data "{\"short_description\":\"can you explain difference between rice bowl and rice ball\"}" \
--user 'admin':'********'

저장하고
:wq!

권한 변경 및 실행
[root@cfebb9fa5330 alertscripts]# chmod 755 aaa.sh
[root@cfebb9fa5330 alertscripts]# ./aaa.sh
{"result":{"parent":"","made_sla":"true","caused_by":"","watch_list":"","upon_reject":"cancel","sys_updated_on":"2018-07-01 06:15:38","child_incidents":"0","hold_reason":"","approval_history":"","skills":"","number":"INC0010131","resolved_by":"","sys_updated_by":"admin","opened_by":{"link":"https://********.service-now.com/api/now/table/sys_user/6816f79cc0a8016401c5a33be04be441","value":"6816f79cc0a8016401c5a33be04be441"},"user_input":"","sys_created_on":"2018-07-01 06:15:38","sys_domain":{"link":"https://********.service-now.com/api/now/table/domain/09ff3d105f231000b12e3572f2b4775d","value":"09ff3d105f231000b12e3572f2b4775d"},"state":"1","task_for":"","sys_created_by":"admin","knowledge":"false","order":"","calendar_stc":"","closed_at":"","u_work_notes":"","cmdb_ci":"","delivery_plan":"","contract":"","impact":"3","active":"true","work_notes_list":"","business_service":"","priority":"5","sys_domain_path":"!!!/!!#/!!!/","rfc":"","time_worked":"","expected_start":"","opened_at":"2018-07-01 06:15:38","business_duration":"","group_list":"","work_end":"","caller_id":"","reopened_time":"","resolved_at":"","approval_set":"","subcategory":"","work_notes":"","short_description":"can you explain difference between rice bowl and rice ball","close_code":"","correlation_display":"","delivery_task":"","work_start":"","assignment_group":"","additional_assignee_list":"","business_stc":"","description":"","calendar_duration":"","close_notes":"","notify":"1","sys_class_name":"incident","closed_by":"","follow_up":"","parent_incident":"","sys_id":"3df6a51d4f031300bac28020a310c7ee","contact_type":"","reopened_by":"","incident_state":"1","urgency":"3","problem_id":"","company":"","reassignment_count":"0","activity_due":"","assigned_to":"","severity":"3","comments":"","approval":"not requested","sla_due":"","comments_and_work_notes":"","due_date":"","sys_mod_count":"0","reopen_count":"0","sys_tags":"","escalation":"0","upon_approval":"proceed","correlation_id":"","location":"","category":"inquiry"}}
[root@cfebb9fa5330 alertscripts]#

Incident를 기표 할 수있는 것 같습니다.



ZABBIX에서 alert가 발생했을 때의 스크립트로 지정하여 ZABBIX와 제휴 등도 할 수 있습니다.

ServiceNow Script(JavaScript)로 자신의 Instance에 대해 실행해 보기



REST API Explorer에서 POST에서 Incident의 Short Description에 값을 추가하는 설정으로 설정합니다 (cURL과 절차는 함께)

보내기를 클릭하여 201에서 성공했는지 확인합니다.

[ServiceNow Script]를 클릭하면 ServiceNow Script Code Sample 대화 상자가 표시됩니다.

[Select Snippet]을 클릭하는 소스가 선택되므로 Ctrl+C로 복사하여

Filter navigator에 Scripts - Background를 입력하는 등 Scripts - Background로 이동하여 Run script의 텍스트 상자에 Ctrl + V로 붙여넣고 var password = 'admin';의 부분을 필요에 따라 편집하고

[Run script]에서 실행

POST 대상 Incident를 확인한 후 레코드가 추가되었습니다.


코피페용


var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://********.service-now.com/api/now/table/incident');
request.setHttpMethod('POST');

//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'admin';
var password = 'admin';

request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');request.setRequestBody("{\"short_description\":\"from localhost\"}");
var response = request.execute();
gs.log(response.getBody());

좋은 웹페이지 즐겨찾기