> For the complete documentation index, see [llms.txt](https://docs.sharelogic.com/unifi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sharelogic.com/unifi/4.4/configure/attachments/sending-multipart-attachments.md).

# Sending Multipart Attachments

## Overview

Some third-party systems only support receiving attachments using **`multipart/form-data`** requests.

Because multipart requests require the payload body, boundaries, and headers to be constructed together, this is implemented using a **Unifi Multipart Helper** service that assembles the multipart body as a temporary attachment, which can then be streamed as the outbound payload.

For more information on general attachment sending, see the [**Sending Attachments**](/unifi/4.4/configure/attachments/sending-attachments.md#streaming-attachments) documentation.

{% hint style="info" %}
The **Unifi Multipart Helper** is provided separately. Please contact us via **support** and we will share it with you.
{% endhint %}

***

## Sending Multipart Attachments

The multipart helper is used from the **Stage to Request** script to construct and stream a multipart request containing one or more attachments.

In this stage, Unifi:

* Collects eligible bonded attachments using `AttachmentSender`
* Builds a multipart body containing those attachments
* Stores the multipart body as a temporary attachment
* Streams that temporary attachment as the outbound request payload

### Stage to Request Script

```javascript
// ------------------------------------------------------------------
// Build and stream a multipart/form-data request containing attachments
// ------------------------------------------------------------------
var mp = new UnifiMultipartHelper();

// Use the current transaction as the host record for temporary
// attachments - these will be cleaned up later by an Event Action
mp.setHostRecord(transaction);

// Add each bonded attachment to the multipart body
var sender = new x_snd_eb.AttachmentSender(transaction, bond);
while (sender.next()) {
  mp.addAttachment('file', sender.attachment_id);
}

// Call the service to create the multipart body as a temporary attachment
var data_attachment_id = mp.createBody();

// Prepare request to stream the temporary attachment
headers['Content-Type'] = mp.getContentType();
payload = 'sys_attachment:' + data_attachment_id;
```

**How this works**

* Each call to `mp.addAttachment()` adds an attachment to the multipart body
* `mp.createBody()` assembles the multipart payload and writes it as a temporary attachment
* Setting the payload to `sys_attachment:<id>` instructs Unifi to **stream** the multipart body
* The `Content-Type` header includes the correct multipart boundary

***

## Cleaning Up Temporary Attachments

The multipart body is stored as a temporary attachment and must be removed once the transaction completes.

This is handled using an **Event Action**.

### Event Action: Remove Temporary Attachments

**Description**\
Automatically remove the temporary attachments created for streaming multipart/form-data content.

**Run Conditions**

* **Event:** `transaction.complete`

**Action Script**

```javascript
// Remove temporary attachments from the current transaction
var mp = new UnifiMultipartHelper();
mp.removeTemporaryAttachments(current);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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.4/configure/attachments/sending-multipart-attachments.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.
