Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This server class allows operating with Use this class to operate the REST API methods and parameters.

Tip

Change the REST_message_record text from the examples below to the record name of your REST message.

Tip

To know more about the sws object, please refer to the SimpleWS article.

SimpleRestRequest()

Initialises an empty SimpleRestRequest object. When using such object, specify the method and the endpoint manually.

SimpleRestRequest(requestName, methodName)

Creates an instance of the SimpleRestRequest object using information from the REST message record. You need to have a REST record before using this constructor.

SimpleRestRequest(requestName, methodName)


With this method you can create an object to use in the REST requests. When using the method, the system creates an object with the sws global variable (see the code example below).


Parameter(s):

NameTypeMandatoryDefault Value
requestNameStringNN
methodNameStringNN


Info

Provide values for parameters as shown below:

ParameterValue
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 to this request.


Return:

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


Example:

Code Block
languagejs
themeEclipse
titleSimpleRestRequestApi
linenumberstrue
/* Create a 'Telegram' request in the REST Requests (sys_rest_requests) table and a 'Send Message' method in the 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();

// 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 this method to prepare a file in binary format and send it in a request.


Parameter(s):

NameTypeMandatoryDefault Value
requestName
Description
contentStringYNThe base64 encoded binary file data.
paramNameStringYN
methodName
The name of the parameter in the POST request that transmits the binary data.
fileNameStringYNThe 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:

TypeDescription
VoidThis method does not return a value.

ExampleExamples:

Code Block
languagejs
themeEclipse
titleSimpleRestRequestApi
const sm = new sws.restRequestV1('REST_message_record', 'get')

execute()

Adding two files
linenumberstrue
const request = sws.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.addFileContent(downloadResponse.getContentBase64(), 'files[2]', 'file2.png');
const uploadResponse = request.execute();



Code Block
languagejs
themeEclipse
titleAdding one file
linenumberstrue
const request = sws.restRequestV1(); 
request.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 requestSends the REST message.


Return:

TypeDescription
SimpleRestResponse
Response to the message sent by the execute() method
This method returns a SimpleRestResponse object.


Example:

Code Block
languagejs
themeEclipse
titleexecute()
linenumberstrue
const smrequest = sws.restRequestV1('slack_platform', 'send_message');
      sm.setRequestBody(json_m);
      sm);
request.setRequestUrl(`https://jsonplaceholder.typicode.com/todos/1`);
request.setRequestMethod('GET');
const response = request.execute();

setBasicAuth(userName,

userPass

userPassword)


Sets the username for web-service auth Use this method to set a username and a password to authorize in a web service if the basic auth authentication type was chosenis selected


Parameter(s):

NameTypeMandatoryDefault Value
userNameStringYN
userPassuserPasswordStringYN


Return:

TypeDescription
VoidThis method does not return a value.


Example:

Code Block
languagejs
themeEclipse
titlesetBasicAuth()
linenumberstrue
const smscriptJSON = {
    'script': 'ss.info(new SimpleDateTime().getValue())'
}
const request = sws.restRequestV1('REST_message_record();
request.setRequestUrl(ss.getProperty('simple.instance.uri') + '/v1/ajax-script/run');
request.setRequestMethod('POST');
request.setBasicAuth('admin', 'password');
request.setRequestHeader('Content-type', 'get');
      sm.setBasicAuth('username', 'password');

setRequestUrl(requestUrl)

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
languagejs
themeEclipse
titleAuthorization method
linenumberstrue
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 a request URL. To get a request URL, use the getRequestUrl() methodSets the request URL.


Parameter(s):

NameTypeMandatoryDefault Value
requestUrlStringYN


Return:

TypeDescription
VoidThis method does not return a value.


Example:

Code Block
languagejs
themeEclipse
titlesetRequestUrl()
linenumberstrue
const smrequest = new sws.restRequestV1('REST_message_record', 'get');
      sm.setRequestUrl(“url”);();
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)


Sets the requesting Use this method to set a request method (GET, POST, PURGE, etc). Accepts the requesting method name. 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:

Code Block
languagejs
themeEclipse
titlesetRequestMethod()
linenumberstrue
const smrequest = new sws.restRequestV1('REST_message_record', 'get');
      sm();
request.setRequestUrl('https://instance.example.com/v1/ajax-script/run');
request.setRequestMethod('get');POST');
ss.info(request.getRequestUrl());
ss.info(request.getRequestMethod());
// Info: https://instance.example.com/v1/ajax-script/run
// Info: POST

setRequestTimeout(timeout)


Sets Use this method to set the response timeout until the request is out of timein seconds.


Parameter(s):

NameTypeMandatoryDefault Value
timeoutIntegerYN


Return:

TypeDescription
VoidThis method does not return a value.


Example:

Code Block
languagejs
themeEclipse
titlesetRequestTimeout()
linenumberstrue
const smrequest = new sws.restRequestV1('REST_message_record', 'get);
request.setRequestUrl('https://instance.example.com/v1/ajax-script/run');
request.setRequestMethod('GET');
request.setRequestTimeout(60);
const response =    sm.setRequestTimeout('60000'request.execute();

setQueryParameter(name, value)


Adds Use this method to add a parameter into the end of the request URL generated as "name=value",.


Parameter(s):

NameTypeMandatoryDefault Value
nameStringYN
valueStringYN


Return:

TypeDescription
VoidThis method does not return a value.


Example:

Code Block
languagejs
themeEclipse
titlesetQueryParameter()
linenumberstrue
const smrequest = new sws.restRequestV1('REST_message_record', 'get();
request.setRequestUrl('https://instance.example.com/rest/v1/table/user');
request.setRequestMethod('GET');
request.setRequestHeader('Authorization', 'Bearer ' +   smnew SimpleUser().getAccessToken());
request.setQueryParameter('sysparm_query', 'active=true^ORDERBYDESCcategorytrue^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 Use this method to set the request body when used 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:

Code Block
languagejs
themeEclipse
titlesetRequestBody()
linenumberstrue
const smrecordURL =
 new   swsss.restRequestV1('REST_message_record', 'get');
let body = '<Message body content>';
      sm.setRequestBody(bodygetProperty('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 Use this method to set a request variable with the name specified from the record to the value specifiedset in the name and the value set in the value parameter.

Parameter(s):

NameTypeMandatoryDefault Value
nameStringYN
valueStringYN


Return:

TypeDescription
VoidThis method does not return a value.


Example:

Code Block
languagejs
themeEclipse
titlesetStringParameter()
linenumberstrue
/* 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();const sm = new sws.RestRequest('<REST_request_record>','get'); 
      sm.setStringParameter('s','NOW');

setRequestHeader(name, value)


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


Parameter(s):

NameTypeMandatoryDefault Value
nameStringYN
valueStringYN


Return:

TypeDescription
VoidThis method does not return a value.


Example:

Code Block
languagejs
themeEclipse
titlesetRequestHeader()
linenumberstrue
const smscriptJSON = {
    'script': 'ss.info(new SimpleDateTime().getValue())'
}
const request = sws.restRequestV1();
request.setRequestUrl('https://instance.example.com/v1/ajax-script/run');
request.setRequestMethod('REST_message_record'POST');
request.setBasicAuth('username', 'get');
      smpassword');
request.setRequestBody(JSON.stringify(scriptJSON));
request.setRequestHeader('AcceptContent-type', 'Applicationapplication/json');
const response = request.execute();


getRequestUrl()


Displays the Use this method to receive a request URL with its parameters. To set a request URL, use thesetRequestUrl(requestUrl) method.


Return:

TypeDescription
StringThe request
URl
URL.


Example:

Code Block
languagejs
themeEclipse
titlegetRequestUrl()
linenumberstrue
const smrequest = new sws.restRequestV1('REST_message_record', 'get);
request.setRequestUrl(ss.getProperty('simple.instance.uri') + '/v1/ajax-script/run');
const url = smrequest.getRequestUrl();
ss.info(url);
// Info: https://your-instance-url/v1/ajax-script/run

getRequestBody()


Returns the request bodyUse this method to return a request body. To set a request body, use the setRequestBody(body) method.


Return:

TypeDescription
StringThe request body.


Example:

Code Block
languagejs
themeEclipse
titlegetRequestBody()
linenumberstrue
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 =const sm = new sws.restRequestV1('REST_message_recordDevOps Team Request', 'get');
letsend_message_to_channel');
request.setRequestBody(JSON.stringify(dataJSON));
request.execute();
const body = smrequest.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()


Returns all the requested headersUse this method to receive all request headers. To set request headers, use the setRequestHeader(name, value) method.


Return:

TypeDescription
Array
ObjectThe
requested headers
keys are the hearder names, the values are the arrays of header values.


Example:

Code Block
languagejs
themeEclipse
titlegetRequestHeaders()
linenumberstrue
const smrequest = new sws.restRequestV1('REST_message_record);
request.setRequestHeader('Content-type', 'getapplication/json');
const headersheader = smrequest.getRequestHeaders();
ss.info(header);
// Info: {"content-type":["application\/json"]}

getRequestMethod()


Returns the requesting methodUse this method to receice the request method used. To set a request method, use setRequestMethod(methodName).


Return:

TypeDescription
StringThe method name.


Example:

Code Block
languagejs
themeEclipse
titlegetRequestMethod()
linenumberstrue
const sm = new sws.restRequestV1('REST_message_record', 'get' request = sws.restRequestV1();
request.setRequestMethod('GET');
const method = request.getRequestMethod();
ss.info(method);
//Info: GET

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

NameTypeMandatoryDefault Value
valueBooleanNtrue

Return:

TypeDescription
VoidThis method does not return a value.

Example:

Code Block
languagejs
themeEclipse
titleuseHttp2()
linenumberstrue
const request = sws.restRequestV1();
request.setRequestUrl('https://http2.pro/api/v1');
request.setRequestMethod('GET');
request.useHttp2();
const methodresponse = sm.getRequestMethod(request.execute();

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

Code Block
languagejs
themeEclipse
titleuseHttp2()
linenumberstrue
request.useHttp2(false);


Table of Contents
absoluteUrltrue
classfixedPosition