Versions Compared

Key

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

Here you can download a file with example configurations for the To try the following scripted REST API : examples, download the [SOC] - Scripted REST API. Import the pack package and import it to your instance and try the following example settings

Example 1. Get a system property value without authorization


To verify try the example: 

  1. Go to the main page of your instance.
  2. Add to the URL bar the following value: /v1/api/c_simple/api_module_path/api_action_path?param_1=value_1.
  3. Copy the result URL.
  4. Go to the URL in Incognito mode.

As a result, you receive a response from to the API action /record/sys_api_action/164054047017513732 with a system property value simple.auth_page.support_phone

Example 2. Create a task record with an attachment


To verify try the example: 

  1. Open the record /record/sys_script/164053985417710860.
  2. Click Run to run the script.

As a result, you receive a response from to the API action /record/sys_api_action/161831494014244852.

On the bottom of the script form, there is a link to the created task record. See the screenshot below:

The record contains two attachments with base 64 the base64 files transferred in lines 14-15 14–15 of the script in Step 1. See the screenshot below:.

Image Added

Example 3. Create an attachment


You can create attachments using Scripted REST API. To pass file content, use the base64 format. To get a file content in base64 format, use the readBase64(attachmentId) method. 

To send an attachment with the Scripted Rest API, do the following:

  1. Create an API action to operate POST requests with the attached content.

    Code Block
    themeEclipse
    titleScript example for an API action
    linenumberstrue
    collapsetrue
    (function (request, response) {
      const data = request.getBody();
      const TABLE_ID = data.table_id;
      const RECORD_ID = data.record_id;
      const RECORD_DOC_ID = ss.getDocIdByIds(TABLE_ID, RECORD_ID);
      const attachments = data.attachments;
      const simpleAttach = new SimpleAttachment();
      let createdAttachmentIds = [];
      Object.keys(attachments).forEach(attachment => {
        createdAttachmentIds.push(simpleAttach.writeBase64(RECORD_DOC_ID, ...attachments[attachment]));
      });
      response.setBody(createdAttachmentIds);
    
    }) (SimpleApiRequest, SimpleApiResponse)
    
    


  2. Create a script to send the requests.

    Code Block
    themeEclipse
    titleA script example to send a POST request
    linenumberstrue
    collapsetrue
    const API_ACTION_URL = ''; // https://docs.simpleone.ru/display/SAPI/Configuring+Scripted+REST+API#ConfiguringScriptedRESTAPI-ScriptedRESTAPIURIs
    
    const payloadRequest = sws.restRequestV1();
    payloadRequest.setRequestUrl(API_ACTION_URL);
    payloadRequest.setRequestMethod('POST');
    payloadRequest.setRequestHeader('Content-type', 'application/json');
    const payload = {
      table_id: '155931135900000084', //  user table ID
      record_id: '155931135900000001', //  admin record ID
     record  attachments: {
        1: ['file_example.gif', 'R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=', 'image/gif']
      }
    }
    
    payloadRequest.setRequestBody(JSON.stringify(payload));
    const response = payloadRequest.execute().getBody();
    print('Response: ' + response);
    
    


For more information see the Configuring Scripted REST API article. Image Removed

Features and recommendations


  1. API action script is executed regardless of the ACL.
  2. Authorization of requests to the Scripted REST API is not possible with Basic Auth. To authorize, use the solution described above in the Authorization section.
  3. A reply response to the API action containing a key error with a value Undefined index: SimpleApiResponse means the script API action contains an error.
  4. To avoid data corruption, convert JSON data type into type String using athe JSON.stringify()method.

  5. If the execution time of the API action script exceeds the Timeout, move a part of the script to the Event Script and call a system event with an API action script.

Table of Contents
absoluteUrltrue
classfixedCondition
printablefalse