Rest Integrations
Rest Integrations
in
REST Integration
Integration: Integration is the ability for different tools to talk to each other and exchange data.
Only one tool will send the data and second tool will receive the data
SOURCE TARGET
SOURCE TARGET
Inbound Integration:
Requests which are incoming to ServiceNow is inbound integration.
Example: Using the inbound API to insert a new incident Request.
Inbound
ServiceNow Third-Party Tools
Outbound Integration:
Requests which are going from ServiceNow is outbound integration
Outbound
ServiceNow Third-Party Tools
An API is set of definitions and protocols that allow one application to communicate with other.
It is not necessary that API means WEB API, We can have API with local files
Example: Jar files, Stream API, Collections and same way other libraries.
WEB API:
Similarly Web Services, Web API is an API available on web.
Web Service:
1. Web Service is a piece of software that makes itself available over the internet.
2. Web Service has collection protocols and standards are used to exchange data between
applications or systems
3. The exchange of data is preferably in standard formats like JSON or XML.
Example:
1. Retrieve information about TVs from Amazon.
2. Submit an order to Amazon
Web service and web API, both are used to communicate for two machines over internet.
1. It is a Protocol
2. Can work on almost any internet protocol
3. Permits only XML.
4. SOAP support SSL and WS-Security
5. It is a Envelope
6. Requires more bandwidth
REST defines a set of functions through which we can send requests and receive responses from other
tools or applications via http protocol
1. REST is an architectural style for building web services.
2. Works mostly HTTP
3. REST uses for all 4 CRUD operations.
4. Requires less bandwidth
5. REST permits not only XML and file types like JSON, HTML
6. URI exposes the business logic
7. Restful web service, expose API from your application in secure, uniform, stateless
manner to the calling client
SOAP REST
1. SOAP is a protocol. 1. REST is an architectural style.
2. SOAP stands for Simple Object Access Protocol. 2. REST stands for Representational State Transfer.
3. REST can use SOAP web services because it is a concept
3. SOAP can't use REST because it is a protocol. and can use any protocol like HTTP, SOAP.
4. SOAP uses services interfaces to expose the
business logic. 4. REST uses URI to expose business logic.
5. JAX-WS is the java API for SOAP web services. 5. JAX-RS is the java API for RESTful web services.
6. SOAP defines standards to be strictly followed. 6. REST does not define too much standards like SOAP.
7. SOAP requires more bandwidth and resource
than REST. 7. REST requires less bandwidth and resource than SOAP.
8. REST permits different data format such as Plain text,
8. SOAP permits XML data format only. HTML, XML, JSON etc.
9. SOAP is less preferred than REST. 9. REST more preferred than SOAP.
Diagram:
+---------------------+
| URN |
+---------------------+
| Namespace: isbn |
| Identifier: 0451450523 |
+---------------------+
Summary Flow
To summarize the relationship between URI, URL, and URN, you can use the following flow:
+---------------------+
| URI |
+---------------------+
| |
| +--------------+ |
| | URL | |
| +--------------+ |
| |
| +--------------+ |
| | URN | |
| +--------------+ |
+---------------------+
Endpoint:
1. Endpoints are the https address, and are used to make a request to get or send data
It is the URL which will accept the web services.
2. Basically endpoint is the address of the application to which the source application wants to
communicate.
Resource:
1. Resource is simply a data in Web Application
2. In REST, the primary data representation is called resource.
3. A resource can be a singleton or a collection.
Example:
“Customers” is a collection resource and “customer” is a singleton resource (in a
banking domain).
We can identify “customers” collection resource using the URI “/customers“.
We can identify a single “customer” resource using the URI “/customers/{customerId}“.
4. A resource may contain sub-collection resources also.
Example:
sub-collection resource “accounts” of a particular “customer” can be identified using the
URN “/customers/{customerId}/accounts” (in a banking domain).
Similarly, a singleton resource “account” inside the sub-collection resource “accounts”
can be identified as follows: “/customers/{customerId}/accounts/{accountId}“.
HTTP Methods:
1. GET: [Retrieve information about the REST API resource.]
1. Use GET requests to retrieve resource information only – and not modify it in any
way. As GET requests do not change the resource’s state, these are said to be safe
methods.
2. Additionally, GET APIs should be idempotent. Making multiple identical requests
must produce the same result every time until another API (POST or PUT) has
changed the state of the resource on the server.
2. POST: Create a REST API resource
1. POST methods are used to create a new resource into the collection of resources.
2. Please note that POST is neither safe nor idempotent, and invoking two identical
POST requests will result in two different resources containing the same information
(except resource ids).
3. POST is used to send data to a server to create/update a resource.
4. The data sent to the server with POST is stored in the request body of the HTTP
request
5. POST requests do not remain in the browser history
6. POST requests cannot be bookmarked
7. POST requests have no restrictions on data length
3. PUT: Update a REST API resource
1. Use PUT APIs primarily to update an existing resource entirely. If the resource does
not exist, then API may decide to create a new resource or not
2. The PUT method is generally called when we need to change a single resource.
3. PUT is used to send data to a server to create/update a resource.
POST PUT
1. PUT methods are used to create/update a new
resource into the collection of resources. But PUT
methods primarily to update an existing resource
1. POST methods are used to create/update a new entirely. If the resource exist, then PUT API update
resource into the collection of resources. But POST the resource, else If the resource does not exist, then
methods primarily use for CREATE operations. API may decide to create a new resource or not
2. The POST method is known to be non-
idempotent. That means, if you retry the request 2. The PUT method is known to be idempotent. That
multiple times, you will end up having multiple is, calling the same PUT request multiple times will
resources with different URIs. always produce the same result.
PUT PATCH
1. APIs primarily to update an existing resource 1. PATCH request method applies partial
entirely. . If the resource exist, then PUT API update modifications to a resource. If you only need to
the resource, or If the resource does not exist, then update one field for the resource, you may want to
API may decide to create a new resource or not use the PATCH method.
2. The PATCH method is known to be non-
2. The PUT method is known to be idempotent.. idempotent. That means, if you retry the request
That is, calling the same PUT request multiple times multiple times, you will end up having multiple
will always produce the same result. resources with different URIs.
3. Whereas, the PATCH method has comparatively
3. The PUT method has high bandwidth. low bandwidth.
Basic Authentication:
Basic authentication is the simplest form of authentication which requires only user name and
password to authenticate and start communication with other systems.
OAuth 2.0:
OAuth 2.0 is the modern form of authentication where IDP provides the token based access
instead of user credentials which additional security parameters like expiry date of token and
limit to only required application/data.
Endpoint:
1. Endpoints are the https address, and are used to make a request to get or send data
It is the URL which will accept the web services.
2. Basically endpoint is the address of the application to which the source application wants to
communicate.
Methods:
1. Methods tell that what source is doing for target instance.
2. Methods are used to perform CURD operations.
3. Methods are types of request like GET, POST,PUT, DELETE or PATCH
Authentication/Authorization details:
1. How the API will be authenticated. Example: Username/Password, Token...etc.
2. To communicate source application to the target application, we need entry pass, means we
need some key or credentials.
3. REST message HTTP methods below authentication types:
a. Basic authentication
b. Mutual (two-way authentication)
c. OAuth 2.0
Content/Request Body: The information which sent along with REST API.
Usually, it is sent with POST and PUT.
Header: HTTP headers let the client and the server pass additional information with HTTP request or
response. Example: Accept, Authorization...etc
Response:
The ServiceNow API response consists of:
1. HTTP Response Status code
2. Response headers
3. Response body
Response Headers:
The Headers section shows the returned headers and their values
Response Body:
The response body is the data object returned by the ServiceNow web service provider.
The response body varies depending on the selected API.
Request Parameters
1. Path parameters:
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.
2. Query parameters:
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.
3. Request headers
Request headers define the format of the Request and Response.
4. Request Body: A Request Body refers to the data that is sent to the server as part of an HTTP request. This is
typically used in POST, PUT, or PATCH requests, where the client needs to send data to the server to create or
update a resource.
1. JSON (JavaScript Object Notation): A common format for sending structured data. For example:
{
"name": "John Doe",
"email": "[email protected]"
}
2. XML (eXtensible Markup Language): Another format for structured data, though less common than JSON in
modern APIs.
<user>
<name>John Doe</name>
<email>[email protected]</email>
</user>
3. Form Data: Often used in web forms, where data is sent as key-value pairs. This can be sent as application/x-
www-form-urlencoded or multipart/form-data.
4. Plain Text: Sometimes, simple text data is sent without any specific structure.
Inbound Integration:
Inbound means that other applications make calls to ServiceNow's REST API to get information from SN,
or create/update records.
Use case:
Through REST integration we will create record in ServiceNow instance from any other tool.
1. HTTP method
2. Endpoint URL
3. Authorization
4. Provide the “Headers”
5. Enter body details in Json format
Outbound Integration:
Outbound means that ServiceNow makes calls to other applications' REST API to get information from
the other application or create/update records.
Use case: ServiceNow to ServiceNow instance
Every time when incident is created in one ServiceNow instance (Source) then incident record
with same information will also get created in another ServiceNow instance (target).
Solution:
This use is implemented through Outbound REST message and Business rule.
Outbound REST message contains required information such endpoint URL, request body and
authorization details and through business rule we are going to trigger the particular rest
message, So that the incident will create in target instance.
In service we need credentials (Username and Password). Source instance need credentials of target
instance user. For that we need to create user with rest_service role and share those details with source
instance.
III. Under API token click “Create and manage API tokens”
IV. Click “Create API token”, then it will ask for “Label” and click “Create”
Endpoint: https://sagaras400.atlassian.net/rest/api/3
Then “Submit”
After submission the rest message by providing the endpoint and authentication, DefaultGET
method create in HTTP methods.
In the HTTP method also we need to provide the HTTP header details under HTTP request.
4. To automate the process we need to write “Business rule” to trigger the Outbound REST
message.
Business rule condition is below
When: async and Insert
Static code:
request.setBasicAuth('[email protected]',
'ATATT3xFfGF0TImb89VZ_qdM8n1Co38t8XIPX2VTT9Aty25np9s_frBWdwguGMry7y0ceVpdjUGV
NNjv7q7jFQYvFBQUcwqudY7ndyE1j_QWebD9rIlVv1o4zYQd-0o95YlIuL4Ct701bnxroIBNK-
Ot2jYMTkzDN8ay8tcewZHbDQTxGI7yDag=E3CB3A77');
request.setRequestHeader('Accept', "application/json");
request.setRequestHeader("Content-Type", "application/json");
})(current, previous);
Dynamic Code:
request.setBasicAuth('[email protected]',
'ATATT3xFfGF0TImb89VZ_qdM8n1Co38t8XIPX2VTT9Aty25np9s_frBWdwguGMry7y0ceVpdjUGV
NNjv7q7jFQYvFBQUcwqudY7ndyE1j_QWebD9rIlVv1o4zYQd-0o95YlIuL4Ct701bnxroIBNK-
Ot2jYMTkzDN8ay8tcewZHbDQTxGI7yDag=E3CB3A77');
request.setRequestHeader('Accept', "application/json");
request.setRequestHeader("Content-Type", "application/json");
})(current, previous);
Bi-directional Webhook:
This has 2 modules:
• Jira Webhook Registries (these are used to assign a token and API path. We need to generate a call
back URL from here and provide it in Jira)
• Jira Webhook Routing Policies(these triggers the sub flows based on the conditions specified)
Note:
• ServiceNow provides a default routing policy which needs to be modified according to the requirement.
• Jira Spoke provides a default sub flow to process the Webhook events
Integration Steps:
• In ServiceNow Instance, Navigate to Integration Hub > Connection & Credential Aliases.
• Click on “Jira” and check that credentials and connections are interconnected with each other.
• There is a OOB decision. Update this with the sub flow that we have created.
https://www.servicenow.com/community/now-platform-articles/integration-with-jira-using-jira-
spoke/ta-p/2323402