Testing ServiceNow Rest Api using CURL

Sometimes you can find very helpful to leave rest api explorer in ServiceNow and use some external tools like Postman or Curl to send some request to your instance. In this post I’m going to present some functionalities of curl.
If your OS is Windows you have to download curl from: https://curl.haxx.se/
Or you can use Git Bash: https://git-scm.com/download/win

Get a Record

To retrieve a specific record you can use the following request:

curl -user:password https://.service-now.com/api/now/table/incident/sys_id

$ curl -u restUser:test1 https://dev46997.service-now.com/api/now/table/incident/d71b3b41c0a8016700a8ef040791e72a
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2965    0  2965    0     0    993      0 --:--:--  0:00:02 --:--:--   993
{  
   "result":{  
      "parent":"",
      "made_sla":"true",
      "caused_by":"",
      "watch_list":"",
      "upon_reject":"cancel",
      "sys_updated_on":"2017-11-22 19:42:25",
      "child_incidents":"",
      "hold_reason":"",
      "approval_history":"",
      "number":"INC0000053",
      "resolved_by":"",
      "sys_updated_by":"admin",
      "opened_by":{  
         "link":"https://dev46997.service-now.com/api/now/table/sys_user/681b365ec0a80164000fb0b05854a0cd",
         "value":"681b365ec0a80164000fb0b05854a0cd"
      },
      "user_input":"",
      "sys_created_on":"2017-11-01 20:48:46",
      "sys_domain":{  
         "link":"https://dev46997.service-now.com/api/now/table/sys_user_group/global",
         "value":"global"
      },
      "state":"2",
      "sys_created_by":"itil",
      "knowledge":"false",
      "order":"",
      "calendar_stc":"",
      "closed_at":"",
      "cmdb_ci":{  
         "link":"https://dev46997.service-now.com/api/now/table/cmdb_ci/26e51a2f0a0a0bb4008628d2254c42db",
         "value":"26e51a2f0a0a0bb4008628d2254c42db"
      },
      "impact":"1",
      "active":"true",
      "work_notes_list":"",
      "business_service":"",
      "priority":"1",
      "sys_domain_path":"/",
      "rfc":"",
      "time_worked":"",
      "expected_start":"",
      "opened_at":"2017-11-01 20:48:46",
      "business_duration":"",
      "group_list":"",
      "work_end":"",
      "caller_id":{  
         "link":"https://dev46997.service-now.com/api/now/table/sys_user/46d4a69ba9fe1981001af9616bf01185",
         "value":"46d4a69ba9fe1981001af9616bf01185"
      },
      "resolved_at":"",
      "approval_set":"",
      "subcategory":"",
      "work_notes":"",
      "short_description":"The SAP HR application is not accessible",
      "close_code":"",
      "correlation_display":"",
      "work_start":"",
      "assignment_group":{  
         "link":"https://dev46997.service-now.com/api/now/table/sys_user_group/8a4dde73c6112278017a6a4baf547aa7",
         "value":"8a4dde73c6112278017a6a4baf547aa7"
      },
      "additional_assignee_list":"",
      "business_stc":"",
      "description":"I've been trying to access the SAP HR application for the last few hours. Each time I go into the application, nothing loads.",
      "calendar_duration":"",
      "close_notes":"",
      "notify":"1",
      "sys_class_name":"incident",
      "closed_by":"",
      "follow_up":"",
      "parent_incident":"",
      "sys_id":"d71b3b41c0a8016700a8ef040791e72a",
      "contact_type":"phone",
      "incident_state":"2",
      "urgency":"1",
      "problem_id":{  
         "link":"https://dev46997.service-now.com/api/now/table/problem/d7296d02c0a801670085e737da016e70",
         "value":"d7296d02c0a801670085e737da016e70"
      },
      "company":{  
         "link":"https://dev46997.service-now.com/api/now/table/core_company/31bea3d53790200044e0bfc8bcbe5dec",
         "value":"31bea3d53790200044e0bfc8bcbe5dec"
      },
      "reassignment_count":"0",
      "activity_due":"2017-11-22 21:42:25",
      "assigned_to":{  
         "link":"https://dev46997.service-now.com/api/now/table/sys_user/46d44a23a9fe19810012d100cca80666",
         "value":"46d44a23a9fe19810012d100cca80666"
      },
      "severity":"3",
      "comments":"",
      "approval":"not requested",
      "sla_due":"2017-11-01 12:42:45",
      "comments_and_work_notes":"",
      "due_date":"",
      "sys_mod_count":"32",
      "reopen_count":"",
      "sys_tags":"",
      "escalation":"3",
      "upon_approval":"proceed",
      "correlation_id":"",
      "location":{  
         "link":"https://dev46997.service-now.com/api/now/table/cmn_location/db96a8480a0a0a65019da337d4a89edc",
         "value":"db96a8480a0a0a65019da337d4a89edc"
      },
      "category":"inquiry"
   }
}

Headers

Use flag -i to print the response header:

curl -i -u restUser:test1 https://dev46997.service-now.com/api/now/table/incident/d71b3b41c0a8016700a8ef040791e72a
HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=C07ABD90116AAB32FB6D2F9D453C378E; Path=/; HttpOnly;Secure
Set-Cookie: glide_user=""; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly;Secure
Set-Cookie: glide_user_session=""; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly;Secure
Set-Cookie: glide_user_route=glide.3be178b1551997a928a6659ee61cb0fa; Expires=Sat, 16-Mar-2086 20:15:22 GMT; Path=/; HttpOnly;Secure
X-Is-Logged-In: true
Set-Cookie: glide_session_store=E40C08D1DB201300F8C85EC0CF9619F4; Expires=Mon, 26-Feb-2018 17:31:15 GMT; Path=/; HttpOnly;Secure
Pragma: no-store,no-cache
Cache-control: no-cache,no-store,must-revalidate,max-age=-1
Expires: 0
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 26 Feb 2018 17:01:14 GMT
Server: ServiceNow
Set-Cookie: BIGipServerpool_dev46997=226591754.2881.0000; path=/; Httponly; Secure
Strict-Transport-Security: max-age=63072000; includeSubDomains

Using flag -I allows you to ask ServiceNow only for headers, you will send head request.

curl -I -u restUser:test1 https://dev46997.service-now.com/api/now/table/incident/d71b3b41c0a8016700a8ef040791e72a

Output

You can save the response in file by using -o flag:

curl -o incident.txt -u restUser:test1 https://dev46997.service-now.com/api/now/table/incident/d71b3b41c0a8016700a8ef040791e72a

Proxy

You can send your request through proxy server:

curl -x 192.189.25.154:3128 -u restUser:test1 https://dev46997.service-now.com/api/now/table/incident/d71b3b41c0a8016700a8ef040791e72a

Or if you proxy needs user authentication:

curl -x 18.217.249.23:3128 --proxy-user snuser:test1 -u restUser:test1 https://dev46997.service-now.com/api/now/table/incident/d71b3b41c0a8016700a8ef040791e72a

Debug

To see details and commands which are send to ServiceNow you can use -v flag.

curl -v -u restUser:test1 https://dev46997.service-now.com/api/now/table/incident/d71b3b41c0a8016700a8ef040791e72a

The output contains a lot of information:

* STATE: INIT => CONNECT handle 0x405f160; line 1392 (connection #-5000)
* Added connection 0. The cache now contains 1 members
* STATE: CONNECT => WAITRESOLVE handle 0x405f160; line 1428 (connection #0)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 103.23.66.118...
* TCP_NODELAY set
* STATE: WAITRESOLVE => WAITCONNECT handle 0x405f160; line 1509 (connection #0)
* Connected to dev46997.service-now.com (103.23.66.118) port 443 (#0)
* STATE: WAITCONNECT => SENDPROTOCONNECT handle 0x405f160; line 1561 (connection #0)
* Marked for [keep alive]: HTTP default
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: C:/Users/Grzes/AppData/Local/Programs/Git/mingw64/ssl/certs/ca-bundle.crt
  CApath: none
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
} [5 bytes data]
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
} [512 bytes data]
* STATE: SENDPROTOCONNECT => PROTOCONNECT handle 0x405f160; line 1575 (connection #0)
{ [5 bytes data]
* TLSv1.2 (IN), TLS handshake, Server hello (2):
{ [87 bytes data]
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* TLSv1.2 (IN), TLS handshake, Certificate (11):
{ [5118 bytes data]
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
{ [333 bytes data]
* TLSv1.2 (IN), TLS handshake, Server finished (14):
{ [4 bytes data]
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
} [70 bytes data]
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
} [1 bytes data]
* TLSv1.2 (OUT), TLS handshake, Finished (20):
} [16 bytes data]
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
{ [1 bytes data]
* TLSv1.2 (IN), TLS handshake, Finished (20):
{ [16 bytes data]
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server did not agree to a protocol
* Server certificate:
*  subject: C=US; ST=California; L=San Diego; O=ServiceNow; CN=*.service-now.com
*  start date: Jan 19 18:40:12 2018 GMT
*  expire date: Oct  1 19:10:11 2018 GMT
*  subjectAltName: host "dev46997.service-now.com" matched cert's "*.service-now.com"
*  issuer: C=US; O=Entrust, Inc.; OU=See www.entrust.net/legal-terms; OU=(c) 2012 Entrust, Inc. - for authorized use only; CN=Entrust Certification Authority - L1K
*  SSL certificate verify ok.
* STATE: PROTOCONNECT => DO handle 0x405f160; line 1596 (connection #0)
* Server auth using Basic with user 'restUser'
} [5 bytes data]
> GET /api/now/table/incident/d71b3b41c0a8016700a8ef040791e72a HTTP/1.1
> Host: dev46997.service-now.com
> Authorization: Basic cmVzdFVzZXI6dGVzdDE=
> User-Agent: curl/7.58.0
> Accept: */*
>
* STATE: DO => DO_DONE handle 0x405f160; line 1658 (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0x405f160; line 1783 (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0x405f160; line 1799 (connection #0)
{ [5 bytes data]
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 200 OK
< Set-Cookie: JSESSIONID=989163C694DAC0044969AABCB058C8C0; Path=/; HttpOnly;Secure
< Set-Cookie: glide_user=""; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly;Secure
< Set-Cookie: glide_user_session=""; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly;Secure
< Set-Cookie: glide_user_route=glide.3be178b1551997a928a6659ee61cb0fa; Expires=Mon, 18-Mar-2086 21:29:06 GMT; Path=/; HttpOnly;Secure
< X-Is-Logged-In: true
< Set-Cookie: glide_session_store=44107AB5DB201300F8C85EC0CF96195D; Expires=Wed, 28-Feb-2018 18:44:59 GMT; Path=/; HttpOnly;Secure
< Pragma: no-store,no-cache
< Cache-control: no-cache,no-store,must-revalidate,max-age=-1
< Expires: 0
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Wed, 28 Feb 2018 18:14:59 GMT
* Server ServiceNow is not blacklisted
< Server: ServiceNow
< Set-Cookie: BIGipServerpool_dev46997=226591754.2881.0000; path=/; Httponly; Secure
< Strict-Transport-Security: max-age=63072000; includeSubDomains
<
{ [688 bytes data]
* STATE: PERFORM => DONE handle 0x405f160; line 1968 (connection #0)
* multi_done
100  2965    0  2965    0     0   1518      0 --:--:--  0:00:01 --:--:--  1518{"result":{"parent":"","made_sla":"true","caused_by":"","watch_list":"","upon_reject":"cancel","sys_updated_on":"2017-11-22 19:42:25","child_incidents":"","hold_reason":"","approval_history":"","number":"INC0000053","resolved_by":"","sys_updated_by":"admin","opened_by":{"link":"https://dev46997.service-now.com/api/now/table/sys_user/681b365ec0a80164000fb0b05854a0cd","value":"681b365ec0a80164000fb0b05854a0cd"},"user_input":"","sys_created_on":"2017-11-01 20:48:46","sys_domain":{"link":"https://dev46997.service-now.com/api/now/table/sys_user_group/global","value":"global"},"state":"2","sys_created_by":"itil","knowledge":"false","order":"","calendar_stc":"","closed_at":"","cmdb_ci":{"link":"https://dev46997.service-now.com/api/now/table/cmdb_ci/26e51a2f0a0a0bb4008628d2254c42db","value":"26e51a2f0a0a0bb4008628d2254c42db"},"impact":"1","active":"true","work_notes_list":"","business_service":"","priority":"1","sys_domain_path":"/","rfc":"","time_worked":"","expected_start":"","opened_at":"2017-11-01 20:48:46","business_duration":"","group_list":"","work_end":"","caller_id":{"link":"https://dev46997.service-now.com/api/now/table/sys_user/46d4a69ba9fe1981001af9616bf01185","value":"46d4a69ba9fe1981001af9616bf01185"},"resolved_at":"","approval_set":"","subcategory":"","work_notes":"","short_description":"The SAP HR application is not accessible","close_code":"","correlation_display":"","work_start":"","assignment_group":{"link":"https://dev46997.service-now.com/api/now/table/sys_user_group/8a4dde73c6112278017a6a4baf547aa7","value":"8a4dde73c6112278017a6a4baf547aa7"},"additional_assignee_list":"","business_stc":"","description":"I've been trying to access the SAP HR application for the last few hours. Each time I go into the application, nothing loads.","calendar_duration":"","close_notes":"","notify":"1","sys_class_name":"incident","closed_by":"","follow_up":"","parent_incident":"","sys_id":"d71b3b41c0a8016700a8ef040791e72a","contact_type":"phone","incident_state":"2","urgency":"1","problem_id":{"link":"https://dev46997.service-now.com/api/now/table/problem/d7296d02c0a801670085e737da016e70","value":"d7296d02c0a801670085e737da016e70"},"company":{"link":"https://dev46997.service-now.com/api/now/table/core_company/31bea3d53790200044e0bfc8bcbe5dec","value":"31bea3d53790200044e0bfc8bcbe5dec"},"reassignment_count":"0","activity_due":"2017-11-22 21:42:25","assigned_to":{"link":"https://dev46997.service-now.com/api/now/table/sys_user/46d44a23a9fe19810012d100cca80666","value":"46d44a23a9fe19810012d100cca80666"},"severity":"3","comments":"","approval":"not requested","sla_due":"2017-11-01 12:42:45","comments_and_work_notes":"","due_date":"","sys_mod_count":"32","reopen_count":"","sys_tags":"","escalation":"3","upon_approval":"proceed","correlation_id":"","location":{"link":"https://dev46997.service-now.com/api/now/table/cmn_location/db96a8480a0a0a65019da337d4a89edc","value":"db96a8480a0a0a65019da337d4a89edc"},"category":"inquiry"}}
* Connection #0 to host dev46997.service-now.com left intact

<br/ >

The most talkative option is –trace-ascii. Request with this flag shows everything that curl sends and receives. To use it you have to provide where the output should be saved:

curl --trace-ascii debug.txt -u restUser:test1 https://dev46997.service-now.com/api/now/table/incident/d71b3b41c0a8016700a8ef040791e72a

Leave a Reply

Your email address will not be published. Required fields are marked *