This server class allows operating with Use this class to operate the REST API methods and parameters.
SimpleRestRequest(
)Initializes an empty SimpleRestRequest object. When using such object, specify the method and the endpoint manually.
SimpleRestRequest(requestName, methodName)
This method is intended to With this method you can create an object used to use in the REST requests. When using the method, the system creates an object is created with the sws prefix global variable (see the code example below for clarification).
Parameter(s):
Name | Type | Mandatory | Default Value |
---|
requestName | String | N | N |
methodName | String | N | N |
Info |
---|
Provide values to for parameters as shown below: Parameter | Value |
---|
requestName | Specify the value of the Name field taken from the record in the REST Requests (sys_rest_requests) table. | methodName | Specify the value of the Name field taken from the record in the REST Request Methods (sys_rest_request_method) table related with to this request. |
|
Return:
object object initiated by the query transmitted; otherwise, it returns an empty SimpleRestRequest object. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | SimpleRestRequestApi |
---|
linenumbers | true |
---|
|
/* Create thea 'Telegram' request in the REST Requests (sys_rest_requests) table
and thea 'Send Message' method in the REST Request Methods (sys_rest_request_method) table related to withthe 'Telegram' request.
Also create 'chat_id' and 'text' Rest Request Method Param (sys_rest_request_method_param) related withto the 'Send Message' method
*/
const request = sws.restRequestV1('Telegram', 'Send Message');
request.setStringParameter('chat_id', '123456789');
request.setStringParameter('text', 'telegram');
const response = request.execute();
// OR
const request = sws.restRequestV1();
request.setRequestUrl('https://api.telegram.org/bot1860462740:AAHyP6BMH2Mh-cXezrTVu2sJUoNYvimMRMQ/sendMessage');
request.setQueryParameter('chat_id', '123456789');
request.setQueryParameter('text', 'telegram');
const response = request.execute(); |
executeaddFileContent(
)content, paramName, fileName)
Use this method to prepare a file in binary format and send it in a
Sends the REST request.Return:
Parameter(s):
Name | Type | Mandatory | Default Value | Description |
---|
SimpleRestResponse | This method returns a SimpleRestResponse object. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | execute |
---|
linenumbers | true |
---|
|
const request = sws.restRequestV1();
request.setRequestUrl(`https://jsonplaceholder.typicode.com/todos/1`);
request.setRequestMethod('GET');
const response = request.execute(); |
setBasicAuth(userName, userPassword)
Sets the username for web-service auth if the basic authentication type was chosen.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|
userName | String | Y | N |
userPassword | String | Y | N |
content | String | Y | N | The base64 encoded binary file data. |
paramName | String | Y | N | The name of the parameter in the POST request that transmits the binary data. |
fileName | String | Y | N | The file name. |
Info |
---|
It is possible to add several files into the same request. In this case, the name of the POST request parameter should be an array of elements. For example, if there is one file, the parameter can be called files. If there are two files, they should be called files[1] and files[2]. |
Return:
Type | Description |
---|
Void | This method does not return a value. |
ExampleExamples:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | setBasicAuthAdding two files |
---|
linenumbers | true |
---|
|
const scriptJSONrequest = {
'script': 'ss.info(new SimpleDateTime().getValue())'
}
const request = sws.restRequestV1();
request.setRequestUrl(ss.getProperty('simple.instance.uri') + '/v1/ajax-script/run');
request.setRequestMethod('POST');
request.setBasicAuth('admin', 'passwordsws.restRequestV1();
request.setRequestUrl('https://instance.example1.com/some/service'); //the URL to download the files
const downloadResponse = request.execute();
request.setRequestUrl('https://instance.example2.com/v1/attachments/upload/task/165469349718887155'); // the URL to upload the files
request.addFileContent(downloadResponse.getContentBase64(), 'files[1]', 'file.png');
request.setRequestHeader('Content-typeaddFileContent(downloadResponse.getContentBase64(), 'files[2]', 'application/jsonfile2.png');
const uploadResponse = request.setRequestBodyexecute(JSON.stringify(scriptJSON));
const response; |
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | Adding one file |
---|
linenumbers | true |
---|
|
const request = requestsws.executerestRequestV1();
ssrequest.info(response.getBody());
// Info: {"messages":[],"status":"OK","data":{"result":null,"info":"Info: 20... |
This code fragment below demonstrates how to retrieve the authorization token with user login and password. The code uses example data for the the setRequestUrl(requestUrl) method and the payload constant. Replace it with the productive data before usage.setRequestUrl('https://instance.example1.com/some/service'); //the URL to download the file
const downloadResponse = request.execute();
request.setRequestUrl('https://instance.example2.com/v1/attachments/upload/task/165469349718887155'); // the URL to upload the file
request.addFileContent(downloadResponse.getContentBase64(), 'files', 'file.mp3');
const uploadResponse = request.execute(); |
execute()
Use this method to send a REST request.
Return:
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | Auth methodexecute() |
---|
linenumbers | true |
---|
|
const request = sws.restRequestV1();
request.setRequestUrl('https`https://instancejsonplaceholder.exampletypicode.com/v1/auth/login'todos/1`);
request.setRequestMethod('POSTGET');
request.setRequestHeader('Content-type', 'application/json');
const payloadresponse = {
'username': 'admin',
'password': 'qwerty123456'
}
request.setRequestBody(JSON.stringify(payload));
const response = request.execute();
ss.info(JSON.parse(response.getBody()).data.auth_key); // Info: 5WvOT9Ejlxd6Gb8bkCWMtG3XXMYcoDDJ |
setRequestUrl(requestUrl)
Sets the request URL. To get request URL, use the getRequestUrl() method.
Parameter(s):
setBasicAuth(userName, userPassword)
Use this method to set a username and a password to authorize in a web service if the basic authentication type is selected.
Parameter(s):
Name | Name | Type | Mandatory | Default Value |
---|
requestUrluserName | String | Y | N |
Return:
Return:
Type | Description |
---|
Void | This method does not return a value. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | setRequestUrlsetBasicAuth() |
---|
linenumbers | true |
---|
|
const scriptJSON = {
'script': 'ss.info(new SimpleDateTime().getValue())'
}
const request = sws.restRequestV1();
request.setRequestUrl(ss.getProperty('https://instance.example.comsimple.instance.uri') + '/v1/ajax-script/run');
ssrequest.info(setRequestMethod('POST');
request.getRequestUrl());
// Info: https://instance.example.com/v1/ajax-script/run |
setRequestMethod(methodName)
Sets the requesting method (GET, POST, PURGE, etc). Specify the method name within the methodName parameter. To get the method name, use the getRequestMethod() method.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|
methodName | String | Y | N |
Return:
Type | Description |
---|
Void | This method does not return a value. |
setBasicAuth('admin', 'password');
request.setRequestHeader('Content-type', 'application/json');
request.setRequestBody(JSON.stringify(scriptJSON));
const response = request.execute();
ss.info(response.getBody());
// Info: {"messages":[],"status":"OK","data":{"result":null,"info":"Info: 20... |
The code example below demonstrates how to retrieve the authorization token with a user login and password. The code uses example data for the the setRequestUrl(requestUrl) method and the payload constant. Replace it with the real data before using it.
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | Authorization method |
---|
linenumbers | true |
---|
|
const request = sws.restRequestV1();
request.setRequestUrl(' |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | setRequestMethod |
---|
linenumbers | true |
---|
|
const request = sws.restRequestV1();
request.setRequestUrl('https://instance.example.com/v1/ajax-script/run');
request.setRequestMethod('POST');
ss.info(request.getRequestUrl());
ss.info(request.getRequestMethod());
// Info: https://instance.example.com/v1/ajax-script/run
// Info: POST |
setRequestTimeout(timeout)
Sets the response timeout (in seconds) until the request is out of time.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|
timeout | Integer | Y | 60 |
Return:
Type | Description |
---|
Void | This method does not return a value. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | setRequestTimeout |
---|
linenumbers | true |
---|
|
const request = sws.restRequestV1();
request.setRequestUrl('https://instance.example.com/v1/ajax-script/run');
request.setRequestMethod('GET');
request.setRequestTimeout(60);
constauth/login');
request.setRequestMethod('POST');
request.setRequestHeader('Content-type', 'application/json');
const payload = {
'username': 'admin',
'password': 'qwerty123456'
}
request.setRequestBody(JSON.stringify(payload));
const response = request.execute(); |
setQueryParameter(name, value)
ss.info(JSON.parse(response.getBody()).data.auth_key); // Info: 5WvOT9Ejlxd6Gb8bkCWMtG3XXMYcoDDJ |
setRequestUrl(requestUrl)
Use this method to set a request URL. To get a request URL, use the getRequestUrl() method.Adds a parameter into the end of the request URL generated as "name=value",
Parameter(s):
Name | Type | Mandatory | Default Value |
---|
namerequestUrl | String | Y | N | value | String | Y | N |
Return:
Type | Description |
---|
Void | This method does not return a value. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | setQueryParametersetRequestUrl() |
---|
linenumbers | true |
---|
|
const request = sws.restRequestV1();
request.setRequestUrl('https://instance.example.com/rest/v1/tableajax-script/userrun');
ss.info(request.setRequestMethodgetRequestUrl('GET'));
request.setRequestHeader('Authorization', 'Bearer ' + new SimpleUser().getAccessToken());
request.setQueryParameter('sysparm_query', 'active=true^emailLIKEyours^ORDERBYDESCcompany');
const url = request.getRequestUrl();
ss.info(url);
// Info: https://instance.example.com/rest/v1/table/user?sysparm_query=active%3Dtrue%5EemailLIKEyours%5EORDERBYDESCcompany |
setRequestBody(body)
Sets the request body when used PUT or POST methods. To get the request body, use the method.
Parameter(s):
// Info: https://instance.example.com/v1/ajax-script/run |
setRequestMethod(methodName)
Use this method to set a request method (GET, POST, PURGE, etc). Specify the method name within the methodName parameter. To get the method name, use the getRequestMethod() method.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|
methodName |
Name | Type | Mandatory | Default Value |
---|
body | String | Y | N |
Return:
Type | Description |
---|
Void | This method does not return a value. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | setRequestBodysetRequestMethod() |
---|
linenumbers | true |
---|
|
const recordURLrequest =
ss.getProperty('simple.instance.uri') + '/record/' + current.getTableName() + '/' + current.sys_id;
const dataJSON = {
'text': `New Request has been assigned to DevOps Team <${recordURL}|View Request>`
};
const request = sws.restRequestV1('DevOps Team Request', 'send_message_to_channel');
request.setRequestBody(JSON.stringify(dataJSON));
const response = request.execute(); |
setStringParameter(name, value)
Sets the request variable with the name specified from the record to the value specified.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|
name | String | Y | N |
value | String | Y | N |
sws.restRequestV1();
request.setRequestUrl('https://instance.example.com/v1/ajax-script/run');
request.setRequestMethod('POST');
ss.info(request.getRequestUrl());
ss.info(request.getRequestMethod());
// Info: https://instance.example.com/v1/ajax-script/run
// Info: POST |
setRequestTimeout(timeout)
Use this method to set the response timeout in seconds.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|
timeout | Integer | Y | N |
Return:
Type | Description |
---|
Void | This method does not return a value. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | setStringParametersetRequestTimeout() |
---|
linenumbers | true |
---|
|
/* Create the 'Telegram'const request in REST Requests (sys_rest_requests) table
and the 'Send Message' method in REST Request Methods (sys_rest_request_method) table related with 'Telegram' request.
Also create 'chat_id' and 'text' Rest Request Method Param (sys_rest_request_method_param) related with 'Send Message' method
*/
= sws.restRequestV1();
request.setRequestUrl('https://instance.example.com/v1/ajax-script/run');
request.setRequestMethod('GET');
request.setRequestTimeout(60);
const response = request.execute(); |
setQueryParameter(name, value)
Use this method to add a parameter into the end of the request URL generated as "name=value".
Parameter(s):
Name | Type | Mandatory | Default Value |
---|
name | String | Y | N |
value | String | Y | N |
Return:
Type | Description |
---|
Void | This method does not return a value. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | setQueryParameter() |
---|
linenumbers | true |
---|
|
const request = sws.restRequestV1('Telegram', 'Send Message');
request.setRequestUrl('https://instance.example.com/rest/v1/table/user');
request.setStringParametersetRequestMethod('chat_id', '123456789'GET');
request.setStringParametersetRequestHeader('textAuthorization', 'telegram');
const response = request.execute(); |
Sets the HTTP header in the request with the value specified. To get request headers, use the getRequestHeaders() method.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|
name | String | Y | N |
value | String | Y | N |
Bearer ' + new SimpleUser().getAccessToken());
request.setQueryParameter('sysparm_query', 'active=true^emailLIKEyours^ORDERBYDESCcompany');
const url = request.getRequestUrl();
ss.info(url);
// Info: https://instance.example.com/rest/v1/table/user?sysparm_query=active%3Dtrue%5EemailLIKEyours%5EORDERBYDESCcompany |
setRequestBody(body)
Use this method to set the request body when using the PUT or POST methods. To get the request body, use the GET method.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|
body | String | Y | N |
Return:
Return:
Type | Description |
---|
Void | This method does not return a value. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | setRequestHeadersetRequestBody() |
---|
linenumbers | true |
---|
|
const scriptJSONrecordURL = {
'script': 'ss.info(new SimpleDateTime().getValue())'
}
const request = sws.restRequestV1();
request.setRequestUrl('https://instance.example.com/v1/ajax-script/run');
request.setRequestMethod('POST');
request.setBasicAuth('username', 'passwordss.getProperty('simple.instance.uri') + '/record/' + current.getTableName() + '/' + current.sys_id;
const dataJSON = {
'text': `New Request has been assigned to DevOps Team <${recordURL}|View Request>`
};
const request = sws.restRequestV1('DevOps Team Request', 'send_message_to_channel');
request.setRequestBody(JSON.stringify(scriptJSONdataJSON));
request.setRequestHeader('Content-type', 'application/json');
const response = request.execute(); |
getRequestUrlsetStringParameter(name, value)
Displays the request URL with parameters. To set request URL, use thesetRequestUrl(requestUrl) method.
Use this method to set a request variable with the name set in the name and the value set in the value parameter.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|
name | String | Y | N |
value | String | Y | N |
Return:
Type | Description |
---|
Void | This method does not return a value. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | setStringParameter() |
---|
linenumbers | true |
---|
|
/* Create a 'Telegram' request in REST Requests (sys_rest_requests) table
and a 'Send Message' method in REST Request Methods (sys_rest_request_method) table related to the 'Telegram' request.
Also create 'chat_id' and 'text' Rest Request Method Param (sys_rest_request_method_param) related to the 'Send Message' method
*/
const request = sws.restRequestV1('Telegram', 'Send Message');
request.setStringParameter('chat_id', '123456789');
request.setStringParameter('text', 'telegram');
const response = request.execute(); |
Use this method to set a HTTP header in the request with the value specified. To get the request headers, use the getRequestHeaders() method.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|
name | String | Y | N |
value | String | Y | N |
Return:
Type | Description |
---|
Void | This method does not return a value. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | setRequestHeader() |
---|
linenumbers | true |
---|
|
const scriptJSON = {
'script': 'ss.info(new SimpleDateTime().getValue())'
}
const request = sws.restRequestV1();
request.setRequestUrl('https://instance.example.com/v1/ajax-script/run');
request.setRequestMethod('POST');
request.setBasicAuth('username', 'password');
request.setRequestBody(JSON.stringify(scriptJSON));
request.setRequestHeader('Content-type', 'application/json');
const response = request.execute(); |
getRequestUrl()
Use this method to receive a request URL with its parameters. To set a request URL, use thesetRequestUrl(requestUrl) method.
Return:
Type | Description |
---|
String | The request URL. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | getRequestUrl() |
---|
linenumbers | true |
---|
|
const request = sws.restRequestV1();
request.setRequestUrl(ss.getProperty('simple.instance.uri') + '/v1/ajax-script/run');
const url = request.getRequestUrl();
ss.info(url);
// Info: https://your-instance-url/v1/ajax-script/run |
getRequestBody()
Use this method to return a request body. To set a request body, use the setRequestBody(body) method.
Return:
Type | Description |
---|
String | The request body. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | getRequestBody() |
---|
linenumbers | true |
---|
|
const recordURL =
ss.getProperty('simple.instance.uri') + '/record/' + current.getTableName() + '/' + current.sys_id;
const dataJSON = {
'text': `New Request has been assigned to DevOps Team <${recordURL}|View Request>`
};
const request = sws.restRequestV1('DevOps Team Request', 'send_message_to_channel');
request.setRequestBody(JSON.stringify(dataJSON));
request.execute();
const body = request.getRequestBody();
ss.info(body);
//Info: {"text":"New Request has been assigned to DevOps Team <https://your-instance-url.simpleone.ru\/record\/task\/161123123123123123|View Request>"} |
Use this method to receive all request headers. To set request headers, use the setRequestHeader(name, value) method.
Return:
Type | Description |
---|
Object | The keys are the hearder names, the values are the arrays of header values. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | getRequestHeaders() |
---|
linenumbers | true |
---|
|
const request = sws.restRequestV1();
request.setRequestHeader('Content-type', 'application/json');
const header = request.getRequestHeaders();
ss.info(header);
// Info: {"content-type":["application\/json"]} |
getRequestMethod()
Use this method to receice the request method used. To set a request method, use setRequestMethod(methodName).
Return:
Type | Description |
---|
String | The method name. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | getRequestMethod() |
---|
linenumbers | true |
---|
|
Return:
Type | Description |
---|
String | The request URL. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | getRequestUrl |
---|
linenumbers | true |
---|
|
const request = sws.restRequestV1();
request.setRequestUrl(ss.getProperty('simple.instance.uri') + '/v1/ajax-script/run');
const url = request.getRequestUrl();
ss.info(url);
// Info: https://your-instance-url/v1/ajax-script/run |
getRequestBody()
Returns the request body. To set request body, use the setRequestBody(body) method.
Return:
Type | Description |
---|
String | The request body. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | getRequestBody |
---|
linenumbers | true |
---|
|
const recordURL =
ss.getProperty('simple.instance.uri') + '/record/' + current.getTableName() + '/' + current.sys_id;
const dataJSON = {
'text': `New Request has been assigned to DevOps Team <${recordURL}|View Request>`
};
const request = sws.restRequestV1('DevOps Team Request', 'send_message_to_channel');
request.setRequestBody(JSON.stringify(dataJSON));
request.execute(setRequestMethod('GET');
const bodymethod = request.getRequestBodygetRequestMethod();
ss.info(bodymethod);
//Info: {"text":"New Request has been assigned to DevOps Team <https://your-instance-url.simpleone.ru\/record\/task\/161123123123123123|View Request>"} |
Returns all request headers. To set request headers, use the setRequestHeader(name, value) method.useHttp2(value)
Use this method to make the HTTP/2 protocol mandatory.
Note |
---|
This methods requires a host supportting the HTTP/2 protocol. |
Parameter(s):
Name | Type | Mandatory | Default Value |
---|
value | Boolean | N | true |
Return:
Array | The requested headersVoid | This method does not return a value. |
Example:
Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | getRequestHeadersuseHttp2() |
---|
linenumbers | true |
---|
|
const request = sws.restRequestV1();
request.setRequestHeader('Content-type', 'application/jsonsetRequestUrl('https://http2.pro/api/v1');
const header = request.getRequestHeaderssetRequestMethod('GET');
ssrequest.infouseHttp2(header);
// Info: {"content-type":["application\/json"]} |
getRequestMethod()
Returns the request method. To set the request method, use the setRequestMethod(methodName) method.
Return:
Type | Description |
---|
String | The method name. |
const response = request.execute(); |
To disable the usage of HTTP/2, to call the method with the false parameter.
Example: Code Block |
---|
language | js |
---|
theme | Eclipse |
---|
title | getRequestMethoduseHttp2() |
---|
linenumbers | true |
---|
|
const request = sws.restRequestV1useHttp2();
request.setRequestMethod('GET');
const method = request.getRequestMethod();
ss.info(method);
//Info: GETfalse); |