Skip to main content
Version: 1.22.3

SimpleRestRequest

This server class provides methods to work with the REST API methods and parameters.

SimpleRestRequest object


To create a SimpleRestRequest object, use the sws.restRequestV1 method as described below.

Parameter(s):

NameTypeMandatoryDefault value
requestNameStringN''
methodNameStringN''
note

Provide values for parameters as shown below:

ParameterValue
requestNameSpecify the value of the Name field taken from the record in the REST Requests (sys_rest_requests) table.
methodNameSpecify the value of the Name field taken from the record in the REST Request Methods (sys_rest_request_method) table related with this request.

Return:

TypeDescription
SimpleRestRequest objectThis method returns a SimpleRestRequest object initiated by the query transmitted; otherwise, it returns an empty SimpleRestRequest object.

Example:

Creating a SimpleRestRequest object
/* Create a 'Telegram' request in the REST Requests (sys_rest_requests) table
and the 'Send Message' method in the REST Request Methods (sys_rest_request_method) table related with the 'Telegram' request.
Also create the 'chat_id' and 'text' Rest Request Method Param (sys_rest_request_method_param) related with 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();

addFileContent(content, paramName, fileName)


Use the method to prepare a file in binary format for sending it in a request.

Parameter(s):

NameTypeMandatoryDefault valueDescription
contentStringYNThe base64 encoded binary file data.
paramNameStringYNThe name of the parameter in the POST request that will transmit the binary data.
fileNameStringYNThe file name.
note

It is possible to add several files to one request. In this case, the name of the POST request parameter should look like 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:

TypeDescription
VoidThis method does not return a value.

Examples:

Adding two files
const request = sws.restRequestV1();
request.setRequestUrl('https://instance.example1.com/some/service'); //the URL from which the files are downloaded
const downloadResponse = request.execute();

request.setRequestUrl('https://instance.example2.com/v1/attachments/upload/task/165469349718887155'); // the URL where to upload files
request.addFileContent(downloadResponse.getContentBase64(), 'files[1]', 'file.png');
request.addFileContent(downloadResponse.getContentBase64(), 'files[2]', 'file2.png');
const uploadResponse = request.execute();
Adding one file
const request = sws.restRequestV1();
request.setRequestUrl('https://instance.example1.com/some/service'); //the URL from which the file is downloaded
const downloadResponse = request.execute();

request.setRequestUrl('https://instance.example2.com/v1/attachments/upload/task/165469349718887155'); // the URL where to upload files
request.addFileContent(downloadResponse.getContentBase64(), 'files', 'file.mp3');
const uploadResponse = request.execute();

execute()


Use the method to end the REST request. You must specify the URLs with the http or https protocols.

Return:

TypeDescription
SimpleRestResponse objectThis method returns a SimpleRestResponse object.

Example:

execute()
const request = sws.restRequestV1();
request.setRequestUrl(`https://jsonplaceholder.typicode.com/todos/1`);
request.setRequestMethod('GET');
const response = request.execute();

setBasicAuth(userName, userPassword)


Use this method to set a user name and password to authorize on a web service if the resource requires the basic authorization.

Parameter(s):

NameTypeMandatoryDefault value
userNameStringYN
userPasswordStringYN

Return:

TypeDescription
VoidThis method does not return a value.

Example:

setBasicAuth()
const scriptJSON = {
'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', '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 script example below shows how to retrieve the authorization token with a user login and password. The script uses example data for the setRequestUrl(requestUrl) method and the payload constant. Replace it with the real data before use this script example.

Auth method
const request = sws.restRequestV1();
request.setRequestUrl('https://instance.example.com/v1/auth/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();
ss.info(JSON.parse(response.getBody()).data.auth_key); // Info: 5WvOT9Ejlxd6Gb8bkCWMtG3XXMYcoDDJ

setRequestUrl(requestUrl)


Use this method to set the request URL. To get a request URL, use the getRequestUrl() method. You must specify the URLs with the http or https protocols.

Parameter(s):

NameTypeMandatoryDefault value
requestUrlStringYN

Return:

TypeDescription
VoidThis method does not return a value.

Example:

setRequestUrl()
const request = sws.restRequestV1();
request.setRequestUrl('https://instance.example.com/v1/ajax-script/run');
ss.info(request.getRequestUrl());
// Info: https://instance.example.com/v1/ajax-script/run

setRequestMethod(methodName)


Use the method to set the request method (GET, POST, PURGE, and others). Specify the method name within the methodName parameter. To get the method name, use the getRequestMethod() method.

Parameter(s):

NameTypeMandatoryDefault value
methodNameStringYN

Return:

TypeDescription
VoidThis method does not return a value.

Example:

setRequestMethod()
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)


Use the method to set the response timeout of the request in seconds.

Parameter(s):

NameTypeMandatoryDefault value
timeoutIntegerYN

Return:

TypeDescription
VoidThis method does not return a value.

Example:

setRequestTimeout()
const request = 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 generated as "name=value" to the end of the request URL.

Parameter(s):

NameTypeMandatoryDefault value
nameStringYN
valueStringYN

Return:

TypeDescription
VoidThis method does not return a value.

Example:

setVasetQueryParameter()
const request = sws.restRequestV1();
request.setRequestUrl('https://instance.example.com/rest/v1/table/user');
request.setRequestMethod('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)


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):

NameTypeMandatoryDefault value
bodyStringYN

Return:

TypeDescription
VoidThis method does not return a value.

Example:

setRequestBody()
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));
const response = request.execute();

setStringParameter(name, value)


Use the method to set the request variable with a name specified in the name parameter into the value parameter.

Parameter(s):

NameTypeMandatoryDefault value
nameStringYN
valueStringYN

Return:

TypeDescription
VoidThis method does not return a value.

Example:

setStringParameter()
/* Create the 'Telegram' request in the **REST Requests** (**sys_rest_requests**) table
and the 'Send Message' method in the **REST Request Methods** (**sys_rest_request_method**) table related with the 'Telegram' request.
Also create the 'chat_id' and 'text' Rest Request Method Param (sys_rest_request_method_param) related with the 'Send Message' method
*/

const request = sws.restRequestV1('Telegram', 'Send Message');
request.setStringParameter('chat_id', '123456789');
request.setStringParameter('text', 'telegram');
const response = request.execute();

setRequestHeader(name, value)


Use the method to set the HTTP header in the request with the value specified. To get request headers, use the getRequestHeaders() method.

Parameter(s):

NameTypeMandatoryDefault value
nameStringYN
valueStringYN

Return:

TypeDescription
VoidThis method does not return a value.

Example:

setRequestHeader()
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 the method to return the request URL with parameters. To set a request URL, use the setRequestUrl(requestUrl) method.

Return:

TypeDescription
StringThis method returns the request URL.

Example:

getRequestUrl()
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 the method to return the request body. To set a request body, use the setRequestBody(body) method.

Return:

TypeDescription
StringThis method returns the request body.

Example:

getRequestBody()
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>"}

getRequestHeaders()


Use the method to return all request headers. To set request headers, use the setRequestHeader(name, value) method.

Return:

TypeDescription
ObjectThe method returns the request header objects. The keys are header names and the values are arrays of header values.

Example:

getRequestHeaders()
const request = sws.restRequestV1();
request.setRequestHeader('Content-type', 'application/json');
const header = request.getRequestHeaders();
ss.info(header);
// Info: {"content-type":["application\/json"]}

getRequestMethod()


Use the method to return the request method. To set the request method, use the setRequestMethod(methodName) method.

Return:

TypeDescription
StringThe method returns the method name.

Example:

getRequestMethod()
const request = sws.restRequestV1();
request.setRequestMethod('GET');
const method = request.getRequestMethod();
ss.info(method);
//Info: GET

useHttp2(value)


Use the method to make use of the HTTP/2 protocol obligatory.

caution

Using this method requires the host that also supports the HTTP/2 protocol.

Parameter(s):

NameTypeMandatoryDefault value
valueBooleanNtrue

Return:

TypeDescription
VoidThis method does not return a value.

Example:

useHttp2()
const request = sws.restRequestV1();
request.setRequestUrl('https://http2.pro/api/v1');
request.setRequestMethod('GET');
request.useHttp2();
const response = request.execute();

To disable the usage of HTTP/2, call the method passing the false parameter.

useHttp2()
request.useHttp2(false);