Search…
Scripted REST Resource
Unifi will automatically generate the Scripted REST Resource to cater for inbound streaming. If using a Resource created on a release prior to v3.0 this will need to be updated manually.
When you create a Process, Unifi will automatically create the corresponding Scripted REST Resources (see Automated Creation of REST Service).
This guide is supplementary to the Bidirectional Asynchronous Incident Guide and assumes the Process (& subsequent Scripted REST Resources) are already in place (See the Web Service page of that guide for details).
We will now examine the automatically generated Attachment Resource. If you are manually updating an existing Resource to cater for inbound streaming, ensure that it looks like the this.

Attachment Resource

The following code is extracted from the automatically generated Attachment Scripted REST Resource:
1
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
2
3
var helper = new x_snd_eb.RestHelper('incident_guides'); // the API name of the Unifi Process
4
processAttachment(helper.getRequest().getRecord());
5
helper.processRequest(request, response, 'POST'); // the HTTP method of the resource
6
7
/**
8
* This scripted REST API allows an attachment to be sent into Unifi in a
9
* similar way to the OOB api/now/attachment REST API, but it processes it
10
* in Unifi.
11
*
12
*/
13
function processAttachment(target_record) {
14
var file_name = request.queryParams.file_name;
15
var reference = request.queryParams.reference;
16
var mime_type = request.getHeader('Content-Type');
17
18
if (!file_name || !reference || !mime_type) return;
19
20
var ga = new GlideSysAttachment();
21
22
// save the body to an attachment on the request
23
var attachment_id = ga.writeContentStream(target_record, file_name, mime_type, request.body.dataStream);
24
25
// return custom payload for the body to process
26
helper._getRequestBody = function _getRequestBody() {
27
return '{\n' +
28
'"message": { "name": "AddAttachment" },\n' +
29
'"reference": "' + reference + '",\n' +
30
'"attachment": "<x-attachment-data sys_id="' + attachment_id + '" />"\n' +
31
'}';
32
};
33
}
34
35
})(request, response);
Copied!
Note: the above script is processing the inbound attachment using the "AddAttachment" message. If your message is named differently then simply update.
You may need to update the api_name in the x_snd_eb.RestHelper() function. It must be the same as the one you set in the Process record.
With the Message & Scripted REST Resource in place, you are now ready to Test the AddAttachment Message.
Export as PDF
Copy link