# Scripted REST Resource

{% hint style="info" %}
When you create a Process, Unifi will automatically create the corresponding Scripted REST Resources (see [Automated Creation of REST Service](/unifi/4.0/configure/process.md#fields)).
{% endhint %}

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](/unifi/4.0/integration-guides/bidirectional-asynchronous-incident-guide/web-service.md) 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:*

```javascript
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var helper = new x_snd_eb.RestHelper('incident_guides'); // the API name of the Unifi Process
    processAttachment(helper.getRequest().getRecord());
    helper.processRequest(request, response, 'POST'); // the HTTP method of the resource

    /**
     * This scripted REST API allows an attachment to be sent into Unifi in a
     * similar way to the OOB api/now/attachment REST API, but it processes it
     * in Unifi.
     *
     */
    function processAttachment(target_record) {
        var file_name = request.queryParams.file_name;
        var reference = request.queryParams.reference;
        var mime_type = request.getHeader('Content-Type');

        if (!file_name || !reference || !mime_type) return;

        var ga = new GlideSysAttachment();

        // save the body to an attachment on the request
        var attachment_id = ga.writeContentStream(target_record, file_name, mime_type, request.body.dataStream);

        // return custom payload for the body to process
        helper._getRequestBody = function _getRequestBody() {
            return '{\n' +
                '"message":    { "name": "AddAttachment" },\n' +
                '"reference":  "' + reference + '",\n' +
                '"attachment": "<x-attachment-data sys_id="' + attachment_id + '" />"\n' +
                '}';
        };
    }

})(request, response);
```

{% hint style="info" %}
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.
{% endhint %}

With the Message & Scripted REST Resource in place, you are now ready to **Test** the **AddAttachment** Message.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sharelogic.com/unifi/4.0/configure/how-to-guides/how-to-handle-attachments/scripted-rest-resource.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
