Versions Compared
Key
- This line was added.
- This line was removed.
- Formatting was changed.
This server class allows operating with 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)
This method is intended to create an object used in the REST requests.
Parameter(s):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.
Name | Type | Mandatory | Default Value |
---|---|---|---|
requestName | String | YNN | '' |
methodName | String | YNN | '' |
Return:
Type | Description |
---|---|
VoidSimpleRestRequest object | This method does not return a valuereturns a SimpleRestRequest object initiated by the query transmitted; otherwise, it returns empty SimpleRestRequest object. |
Example:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
constlet smrequest = new sws.restRequestV1(); // OR let request = sws.restRequestV1('REST_message_recordCustomer Requests Notifier', 'get')send.direct.message'); // 'Customer Requests Notifier' - name of sys_rest_request // 'send.direct.message' - name of sys_rest_request_method |
execute()
Sends the REST messagerequest.
Return:
Type | Description |
---|---|
SimpleRestResponse | Response to the message sent by the execute() method. |
Example:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
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,
userPassuserPassword)
Sets the username for web-service auth if the basic auth type was chosen.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|---|---|---|
userName | String | Y | N |
userPassuserPassword | String | Y | N |
Return:
Type | Description |
---|---|
Void | This method does not return a value. |
Example:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
const smscriptJSON = { "script": "ss.info(new SimpleDateTime().getValue())" } const request = sws.restRequestV1('REST_message_record(); request.setRequestUrl('https://your-instance-url/v1/ajax-script/run'); request.setRequestMethod('POST'); request.setBasicAuth('admin', 'get'password'); request.setRequestBody(JSON.stringify(scriptJSON)); sm.setBasicAuth('usernamerequest.setRequestHeader('Content-type', 'password');application/json'); const response = request.execute(); ss.info(response.getBody()); // Info: {"messages":[],"status":"OK","data":{"result":null,"info":"Info: 20... |
setRequestUrl(requestUrl)
Sets the request URL.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|---|---|---|
requestUrl | String | Y | N |
Return:
Type | Description |
---|---|
Void | This method does not return a value. |
Example:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
const smrequest = new sws.restRequestV1('REST_message_record', 'get'); smrequest.setRequestUrl(“url”'https://your-instance-url/v1/ajax-script/run'); |
setRequestMethod(methodName)
Sets the requesting method (GET, POST, PURGE, etc). Accepts the requesting method name.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|---|---|---|
methodName | String | Y | N |
Return:
Type | Description |
---|---|
Void | This method does not return a value. |
Example:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
const smrequest = new sws.restRequestV1('REST_message_record', 'get'); sm(); request.setRequestUrl('https://your-instance-url/v1/ajax-script/run'); request.setRequestMethod('getPOST'); |
setRequestTimeout(timeout)
Sets the response timeout until the request is out of time.
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 | ||||||
---|---|---|---|---|---|---|
| ||||||
const smrequest = new sws.restRequestV1('REST_message_record', 'get'); sm.setRequestTimeout('60000'); request.setRequestUrl(`https://jsonplaceholder.typicode.com/todos/1`); request.setRequestMethod('GET'); request.setRequestTimeout(60); const response = request.execute(); |
setQueryParameter(name, value)
Adds 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 | ||||||
---|---|---|---|---|---|---|
| ||||||
const sm = new sws.restRequestV1('REST_message_record', 'get'); sm.setQueryParameter('sysparm_query', 'active=true^ORDERBYDESCcategory'); |
setRequestBody(body)
Sets the request body when used PUT or POST methods.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|---|---|---|
body | String | Y | N |
Return:
Type | Description |
---|---|
Void | This method does not return a value. |
Example:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
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)); 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 |
Return:
Type | Description |
---|---|
Void | This method does not return a value. |
Example:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
const sm = new sws.RestRequest('<REST_request_record>','get'); sm.setStringParameter('s','NOW'); |
setRequestHeader(name, value)
Sets the HTTP header in the request for the value specified.
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 | ||||||
---|---|---|---|---|---|---|
| ||||||
const smscriptJSON = { "script": "ss.info(new SimpleDateTime().getValue())" } const request = sws.restRequestV1('REST_message_record(); request.setRequestUrl('https://your-instance-url/v1/ajax-script/run'); request.setRequestMethod('POST'); request.setBasicAuth('admin', 'getpassword'); smrequest.setRequestBody(JSON.stringify(scriptJSON)); request.setRequestHeader('AcceptContent-type', 'Applicationapplication/json'); request.execute(); |
getRequestUrl()
Displays the request URL with parameters.
Return:
Type | Description |
---|---|
String | The request URl. |
Example:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
const sm = new sws.restRequestV1('REST_message_record', 'get'); const url = sm.getRequestUrl(); |
getRequestBody()
Returns the request body.
Return:
Type | Description |
---|---|
String | The request body. |
Example:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
const sm = new sws.restRequestV1('REST_message_record', 'get'); let body = sm.getRequestBody(); |
getRequestHeaders()
Returns all the requested headers.
Return:
Type | Description |
---|---|
Array | The requested headers. |
Example:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
const sm = new sws.restRequestV1('REST_message_record', 'get'); const headers = sm.getRequestHeaders(); |
getRequestMethod()
Returns the requesting method
Return:
Type | Description |
---|---|
String | The method name. |
Example:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
const sm = new sws.restRequestV1('REST_message_record', 'get'); const method = sm.getRequestMethod(); |
Table of Contents | ||||
---|---|---|---|---|
|