REST Flashcards

1
Q

Basic image of how a REST call works

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In an inbound request, a third-party application requests an action through a ServiceNow API. Example ServiceNow APIs include:

  • Table API:
  • Attachment API:
  • Email API:
A
  • Table API: Create, read, update, and delete records from a table
  • Attachment API: Upload and query file attachments
  • Email API: Send and receive email messages using REST
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
A

The fields in the Prepare request section of the REST API Explorer form are determined by which Namespace, API Name, API Version, and REST method is selected.

  • Namespace: Select the web service scope
  • global: Globally scoped APIs
  • now: REST APIs provided by ServiceNow
  • private_scope_name: Namespace format used for APIs (scripted web services) in privately-scoped applications
  • API Name: Select an API to configure and test in the REST API Explorer
  • API Version: Select a specific API version or choose latest
  • Method: Select from the list of available REST methods based on the Namespace, API Name, and API Version. The arrowhead indicates the selected method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Request Parameters consist of:

  • _
  • _
  • _
A

Request Parameters consist of:

  • Path parameters
  • Query parameters
  • Request headers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The list of path parameters depends on the endpoint URL. Path parameters are enclosed in curly braces in the endpoint URL. The values set in the path parameter field are substituted into the endpoint URL when a request is sent.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Request parameters are added to the endpoint URL by the REST API Explorer when the request is sent. The query parameters are specific to the selected API method.

What are the Query Parameters:

A

A default set of query parameters are displayed for the API. To add additional query parameters, use the Add query parameter button to add a new parameter to the query. For a complete list and detailed description of an API’s query parameters, select the API documentation menu item from the REST API Explorer menu.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Request headers define the format of the Request and Response.

Use the Add header button to add additional headers to the request. For the ServiceNow APIs, two useful additional header parameters are:

X-WantSessionNotificationMessages: Set to true to return notifications that have not already been consumed for the existing session.
X-WantSessionDebugMessages: Enable Session Debug and set the header value to true to return session debug logs.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The REST API Explorer responds as if the request came from a third party application:

Request
Response
Response Body

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The ServiceNow API Response consists of:

  • _
  • _
  • _
A
  • HTTP status code
  • Response headers
  • Response body
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the standard HTTP status codes that are used?

  • _
  • _
  • _
  • _
  • _
A

HTTP Status Code

ServiceNow APIs return standard HTTP status codes. Generally speaking:

  • 1xx: Informational
  • 2xx: Success
  • 3xx: Redirection
  • 4xx: Client Error
  • 5xx: Server Error

The response codes indicate the success or failure of the transaction.
The HTTP status codes refer to the interaction with the REST service provider. The status codes do not tell anything about the requested data. The REST transaction request can complete successfully even if no data is returned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Response Headers
The Headers section shows the returned headers and ___ ___.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is the Response Body

A

The response body is the data object returned by the ServiceNow web service provider. The response body varies depending on the selected API. In the example, the Aggregate API returns the count of open incident records in the past year with a priority of Critical or High. The results are grouped by the user in the Assigned to field.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why would a user have the “Web service access only” checbox selected on their user profile?

A

Create an API Request User
Users with the Web service access only option set on their user record cannot log into the ServiceNow UI. This option allows the user credentials to be used only to authorize API connections. To set this option, open the user record for editing using the User Administration > Users module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What should be done to protect tables from REST requests?

A

Administrators can disable web service access to tables. On the table record, open the Application Access section and de-select the Allow access to this table via web services option. REST requests are not accepted for tables unless this option is selected (checked). To set this option, open the table record for editing using the System Definition > Tables module.

IMPORTANT: The REST API Explorer ignores this setting. The REST API Explorer can interact with tables with the Allow access to this table via web services option disabled.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are CORS Rules

A

Cross-Origin Resource Sharing (CORS) rules control which domains can access specific REST API endpoints. To create a CORS rule, use the All menu to open System Web Services > REST > CORS Rules.

In the example, the resource https://www.test-cors.org can only access the Table API using the GET method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
  • REST API:
  • Domain:
  • Max age:
  • HTTP Methods:
  • HTTP Headers:
A
  • REST API: The REST API the CORS rule applies to.
  • Domain: The domain for the CORS rule. Specify the domain using an IP Address or a domain pattern.
  • Max age: The number of seconds to cache the client session. After an initial CORS request, further requests from the same client within the specified time do not require a preflight message. If a value is not specified, the default value of 0 indicates that all requests require a preflight message.
  • HTTP Methods: The methods allowed.
  • HTTP Headers: A comma-separated list of HTTP headers to send in the response. Specified headers are added to the Access-Control-Expose-Headers header.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

There are a number of requirements for specifying the domain including:

  • _
  • _
  • _
A

There are a number of requirements for specifying the domain including:

  • Start with http:// or https://
  • Must be an IP address or domain pattern
  • Can contain only one wildcard *
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

The REST API Explorer creates code samples for integrating with the ServiceNow APIs in several commonly used languages:

A

The REST API Explorer creates code samples for integrating with the ServiceNow APIs in several commonly used languages:

ServiceNow Script
cURL
Python
Ruby
JavaScript
Perl
Powershell

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

QUESTION: True or False? In an inbound REST integration, ServiceNow is the web service provider.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

QUESTION: Which of the following is the application in ServiceNow used to construct and test API requests to a ServiceNow instance?
1. REST API Provider
2. REST API Explorer
3. REST Message Explorer
4. REST Request Builder
5. REST Request Explorer

A

ANSWER: The correct response is 2. REST API Explorer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

QUESTION: Which one of the following is NOT selected to determine what fields are available in the Prepare request section of the REST API Explorer?
1. Path parameters
2. Method
3. API Name
4. Namespace
5. API Version

A

ANSWER: The correct response is 1. Path parameters. Select the Namespace, API Name, API Version, and Method to determine which fields are available in the Prepare request section.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

QUESTION: The highlighted items are examples of which type of values?
The image shows a URL from the Table API with a PUT method that accesses /api/now/table/{tableName}/{sys_id} with {tableName}/{sys_id} highlighted.
* Request parameters
* Request headers
* Query parameters
* Path parameters
* Response headers

A

ANSWER: The correct response is 4. Path parameters.

25
Q

QUESTION: True or False? All query parameters accept encoded queries as values.

A

ANSWER: The correct response is false. Query parameters can contain different types of values, such as encoded queries, field names, or true/false/all values.

26
Q

QUESTION: Which one of the following describes the process to test the configured web service call to a ServiceNow instance?
* Click the Send button and view the results.
* Click the Test button and view the results.
* Click the Execute Now button and view the results.
* Click the Run Test button and view the results.
* Click the Test button, configure the input values, click the Run Test button, and view the results.

A

ANSWER: The correct response is 1. REST API Explorer uses a Send button to initiate the test.

27
Q

QUESTION: Which HTTP Status Code typically means success?
1xx
2xx
3xx
4xx
5xx

A

ANSWER: The correct response is 2. 2xx. Generally speaking:
1xx: Informational
2xx: Success
3xx: Redirection
4xx: Client Error
5xx: Server Error

28
Q

QUESTION: Which of the following are options for how the response is formatted? More than one response may be correct.
1. application/json
2. text/json
3. application/xml
4. text/xml
5. text/csv

A
  • application/json
  • application/xml
  • text/xml

ANSWER: The correct responses are 1,3, and 4.

29
Q

QUESTION: Which of the following are strategies specific to adding security to inbound REST requests? More than one response may be correct.
* Use a really complex password.
* Create a user specifically for inbound requests.
* Disallow web service access to tables.
* Create CORS rules.
* Configure Access Controls to deny access.

A
  • Create a user specifically for inbound requests.
  • Disallow web service access to tables.
  • Create CORS rules.

ANSWER: The correct responses are 2, 3, and 4.

30
Q

QUESTION: REST API Explorer creates code samples for which of the following languages? More than one response may be correct.
* cURL
* Perl
* Python
* JavaScript
* PowerShell

A

cURL
Perl
Python
JavaScript
PowerShell

ANSWER: All of the responses are correct. In addition to the languages listed, REST API Explorer can generate code samples in ServiceNow Script and Ruby.

31
Q

The Content Negotiation section defines the supported request and response formats. The default for the request and response is to allow:

  • _
  • _
  • _
A
  • application/json
  • application/xml
  • text/xml
32
Q

what is a REST request header?

A

A REST request header contains parameters (metadata) that define the HTTP(S) interaction. Commonly used REST headers include:

Authorization
Accept
Content-Type

33
Q

Scripted REST header example:

A
  • Header name: HTTP header field name.
  • API definition: API the header is part of.
  • Short description: Description of what information should be passed in the header.
  • Example value: Sample of the data to be passed to demonstrate how to use the header.
34
Q

What do Query parameters do?

A

Query parameters control what information developers using the API can pass in the API request URL. In the Query Parameters related list, click the New button to create a parameter.

35
Q

Example query parameters

A
  • Query parameter name: Name of the parameter. By convention, query parameters are lowercase and use underscores in place of spaces. Many query parameters in baseline ServiceNow Scripted REST APIs start with the string sysparm_.
  • API definition: API the query is part of.
  • Short description: Description of what information should be passed in the query.
  • Example value: A sample of the data to be passed to demonstrate how to use the query.
36
Q

What is XML?

A

XML - Extensible Markup Language - is a markup language and file format for storing, transmittting, and reconstructing arbitrary data

37
Q

What is HTTP?

A

HTTP - Hypertext Transfer Protocol - “stateless” request/response system

38
Q

What is REST?

A

REST - REpresentational State Transfer - an architectural style for providing standards between computer systems on the web

39
Q

What is an API

A

API - Application Programming Interface - is a way for two or more computer progroms or components to communicate with each other

40
Q

What is json?

A

json - JavaScript Object Notation - lightweight format for storing and transporting data

41
Q

What are the five most common HTTP methods used with REST APIs?

A
  • Get - Retrieve data on a server
  • Post - Create new resources
  • Put - Replace an existing resource with an updated version
  • Patch - Update an existing resource (similar to put but allows some properties to be updated without overwriting others such as updating some fields but not all fields)
  • Delete - Remove data from a database
42
Q

What are the properties of the request object?

A

body
pathParams
queryParams
queryString
uri
url
headers
getHeader()
getSupportedResponseContentTypes()

43
Q

What is the purpose of the body object

A

Provide access to the request body:

//get instance of RESTAPIRequestBody
var requestBody = request.body;

44
Q

What is the purpose of the pathParams object

A

The pathParams object allows script access to path parameters passed in the request URL. The available path parameters are determined by the Scripted REST Service resources.

The userinfo resource URL from the Demo Service is:

https://<instance>/api/<namespace>/demo_service/userinfo/{user_id}</namespace></instance>

The path parameters are passed in when the service is invoked.

//get pathParams object
var pathparams = request.pathParams;
//get user_id property value from pathparams object
var userID = pathparams.user_id;

45
Q

What is the purpose of the queryParams object

A

The queryParams object allows script access to the query parameters from the web service request.

https://<instance_rest_endpoint>/?demo_query=active%3Dtrue
The demo_query parameter value is passed in when the service is invoked.</instance_rest_endpoint>

//get queryParams object
var queryparams = request.queryParams;
//value of myQueryParam is active=true
var myQueryParam = queryparams.demo_query;

The value of the myQueryParam variable after the example script executes is active=true.

46
Q

What is the purpose of the queryString

A

The queryString contains the entire query added to the endpoint URI. The Demo Service URL is:

https://<instance>/api/<namespace>/demo_service/userinfo/5137153cc611227c000bbd1bd8cd2005d?demo_query=active%3Dtrue</namespace></instance>

//get the query string

//value of query is demo_query=active%3Dtrue
var query = request.queryString;
The value of the query variable after the example script executes is demo_query=active%3Dtrue.

47
Q

What is the purpose of the uri

A

The uri string contains the request URI, excluding domain information. The userinfo resource URL from the Demo Service is:

https://<instance>/api/<namespace>/demo_service/userinfo/{user_id}</namespace></instance>

The URI does not include the query parameters.

//get the uri string
//value of query is /api/<namespace>/demo_service/userinfo/5137153cc611227c000bbd1bd8cd2005d
var query = request.uri;
The value of the query variable after the example script executes is /api/187049/demo_service/userinfo/5137153cc611227c000bbd1bd8cd2005d.</namespace>

48
Q

What is the purpose of the url

A

The url string contains the entire request URL

//get the url string
//returns https://instance/api/<namespace>/demo_service/userinfo/5137153cc611227c000bbd1bd8cd2005d
var query = request.url;
The value of the query variable after the script executes is the complete URL.
The query variable contains the entire URL for the request.</namespace>

49
Q

What is the purpose of the headers object

A

The headers object contains all headers property value pairs from the request

//get the headers from the request
var headers = request.headers;
//get the value of the Accept property
var acceptHeader = headers.accept;

The properties of the request.headers object can be different for different APIs. The request.headers properties for the Demo Service API include accept, from, and content-type. Notice that all request.headers property names are lowercase even if defined in the API using uppercase characters. The script used to log the request.headers property also logged the data type. Notice that all properties are strings.

50
Q

what is the purpose of the getSupportedResponseContentTypes() method

A

The getSupportedResponseContentTypes() method returns an array of string values where each string is a content type, such as application/json.

var contentTypes = [];
contentTypes = request.getSupportedResponseContentTypes();
for(i=0;i<contentTypes.length;i++){

gs.info("content type ["  + i + "] = " + contentTypes[i]);   } The contentTypes array has one element for the Demo Service.
51
Q

The RESTAPIRequestBody API includes:

A

data: The request body content as a single object or array of objects.
dataStream: The content of the request body as a stream.
dataString: The content of the request body as a string.
hasNext(): Returns true if the request body contains another entry.
nextEntry(): Retrieves one entry from the request body as a script object.

52
Q

The RESTAPIResponse API includes:

A

The RESTAPIResponse API includes:

  • getStreamWriter(): The ResponseStreamWriter for the response. Use this object to write directly to the response stream.
  • setBody(): Creates the response body, as a JavaScript object. The body content is automatically serialized to JSON or XML depending on the value of the Accept header, passed in the request.
  • setContentType(): Assigns a value, such as application/json, to the Content-Type header in the web service response.
  • setError(): Sets the properties of the response error object when an error is returned.
  • setHeader(): Assigns a value to a REST service response header.
  • setHeaders(): Sets the headers for the web service response.
  • setLocation(): Assigns a value to the Location header in the web service response. See the W3 Location header documentation for more information about this header.
  • setStatus(): Sets the status code to send in the response, such as 200 to indicate success.
53
Q

What will this return:

(function process(/RESTAPIRequest/ request, /RESTAPIResponse/ response) {

// implement resource here

// Get value from the user_id path parameter passed in the URL
var requestUser = request.pathParams.user_id;
// Get value of the demo_query query parameter passed in the URL
var requestDemoQuery = request.queryParams.demo_query;

// Query the sys_user table to get the user record for the user passed in
// the user_id path parameter
var requestUserName = new GlideRecord(‘sys_user’);
requestUserName.get(requestUser);

// Aggregation 1: Incident table
// Get the count of Incident table records where the user from the user_id path
// parameter is the Caller.
var userIncidentCount = new GlideAggregate(‘incident’);
userIncidentCount.addAggregate(‘COUNT’);
userIncidentCount.addQuery(‘caller_id’,requestUser);
userIncidentCount.addEncodedQuery(requestDemoQuery);
userIncidentCount.query();

var incidents = 0;
if (userIncidentCount.next()) {
incidents = userIncidentCount.getAggregate(‘COUNT’);
}

// Aggregation 2: Change request table
// Get the count of Change request table records where the user from the user_id path
// parameter is the Requested by.
var userChangeCount = new GlideAggregate(‘change_request’);
userChangeCount.addAggregate(‘COUNT’);
userChangeCount.addQuery(‘requested_by’,requestUser);
userChangeCount.addEncodedQuery(requestDemoQuery);
userChangeCount.query();

var changes = 0;
if (userChangeCount.next()) {
changes = userChangeCount.getAggregate(‘COUNT’);
}

// Aggregation 3: Problem table
// Get the count of Problem table records where the user from the user_id path
// parameter is the Opened by.
var userProblemCount = new GlideAggregate(‘problem’);
userProblemCount.addAggregate(‘COUNT’);
userProblemCount.addQuery(‘opened_by’,requestUser);
userProblemCount.addEncodedQuery(requestDemoQuery);
userProblemCount.query();

var problems = 0;
if (userProblemCount.next()) {
problems = userProblemCount.getAggregate(‘COUNT’);
}

//Create a body object. Add property value pairs to the body.
var body = {};
body.numInc = incidents;
body.numChg = changes;
body.numPrb = problems;
body.user = {“User name”: requestUserName.user_name,
“User ID”: requestUser};

// Send the response object, which is returned to the requestor, to the body object.
response.setBody(body);

})(request, response);

A
54
Q

Some Things to Remember about Resource Scripts:

The process function is ___-___.
The request and response objects are ___ ___.
Access controls apply to Scripted REST APIs. The user making the request through the API’s authentication ___ ___ ___ to the requested information.

A

Some Things to Remember about Resource Scripts:

The process function is self-invoking.
The request and response objects are automatically instantiated.
Access controls apply to Scripted REST APIs. The user making the request through the API’s authentication must have access to the requested information.

55
Q

In the Request section, notice the format of the request. Can you locate the path parameter and the query parameter?

A
56
Q

The REST Message record includes:

A
  • Endpoint
  • Authentication
  • HTTP Headers
  • HTTP Methods
57
Q

How can a rest call tell the provider what kind of response you would like to receive?

A

One is to append /json at the end of any request:

$ curl ipinfo.io/json
$ curl ipinfo.io/8.8.8.8/json

The other is to set the Accept header to application/json:

$ curl -H “Accept: application/json” ipinfo.io
$ curl -H “Accept: application/json” ipinfo.io/8.8.8.8

58
Q
A