# Uploading Files in Actions

## General

In order to upload files through an action, either a public URL or a URL that is available in a Flow (e.g. the output of the [CSV Helper](/connectors/helpers/csv-helper.md#writing-a-csv-file)) needs to be referenced to in a field with the key `request_binary_url`, which can be configured like this in the [UI Form Schema Renderer](/connectors/building-connectors/building-connector-actions.md#ui-form-schema-rendering):

```json
{
  "request_binary_url": {
    "title": "File URL where the file is fetched from",
    "type": "text",
    "required": false,
    "info": "The file can come from a previous step of a Locoia flow or from a static location on the web"
  }
}
```

{% hint style="info" %}
Only the key (`request_binary_url`) needs to be exactly as in the example, all other parameters can be set according to the action.
{% endhint %}

If that key is set and a valid URL is passed to it, the file will be converted to binary format and sent with the header value `"Content-Type": "multipart/form-data"`.

The body consists of

* `name` which is by default `file`,
* `filename` which is based on the file name in the URL or if that cannot be retrieved `file`,
* `Content-Type` which is based on the file itself, and
* the file itself in binary format

and looks like this (without the file):

```http
Content-Disposition: form-data; name="file"; filename="example-image.png".
Content-Type: image/png.
```

If the endpoint expects the file to be uploaded in that way, both the Request Body and Header Template need to be configured with `{}` only.

### Body parameters

In addition to the automatically set body key-value pair, additional ones can be sent (which is e.g. required by [HubSpot](https://developers.hubspot.com/docs/api/files/files)), by adding the parameters to the body like regular parameters. These will then be sent as `form-data` as well.

These parameters will then have the `filename` parameter, equal to the name of the Parameter. In case that is not desired or accepted by the API, the header should be set to:

```json
{
  "Content-Type": "multipart/form-data"
}
```

### File meta data

The following file meta data can be used in the body or header by referencing to `binary_file.`&#x20;

* `name`
* `content`
* `mime_type`

For an example usage, [see the Gmail action](#upload).

### Alternative `Content-Type` header

To use a different `Content-Type` than the standard `multipart/form-data`, the Request Body Template needs to be left empty and the Content-Type that should be used needs to be set in the Request Header Template.

If the Request Header Template is left empty, no `Content-Type` header will be sent.\
This is e.g. needed for [SharePoint file upload](https://docs.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0\&tabs=http#http-request-to-upload-a-new-file) (and other Microsoft file upload endpoints.

## Additional parameters

### `form_item_name`

Some APIs, such as [this one](https://docs.pricehubble.com/apis/exclusive/comparables/images/) require that the `name` in the body is set to something else than the default `file`, in that case `image`. This can be configured directly in the UI Form Schema, by specifying the parameter `form_item_name` with the `name` that should be sent, e.g.:

```json
  {
  "request_binary_url": {
    ...,
    "form_item_name": "image"
  }
}
```

### `upload`

This parameter can be set to `false` if the file should not actually be uploaded, but only be converted to binary format and then uploaded in another format. This is for example needed to [send emails with attachments with Gmail](https://developers.google.com/gmail/api/reference/rest/v1/users.messages/send), where the request body needs to be built in the request body template and then base64 encoded:

```django
{% set boundary = str(uuid(4)) %}
{% set raw = [
  'Content-Type: multipart/mixed; boundary="' + boundary + '"\n',
  'To: <' + to_email + '>\n',
  ...,
  '--' + boundary + '\n',
  'Content-Type: text/' + body_type + '; charset="UTF-8"\n\n',
  body,
] %}
{% if binary_file != None %}
  {% set raw = raw + [
    '\n\n--' + boundary + '\n',
    'Content-Type: ' + binary_file.mime_type + '; name="' + binary_file.name + '"\n',
    'Content-Disposition: attachment; filename="' + binary_file.name + '"\n',
    'Content-Transfer-Encoding: base64\n\n',
    binary_file.content | base64_encode
  ] %}
{% endif %}
{% set raw = raw + ['\n\n--' + boundary + '--'] %}
{
  "raw": "{{ raw | join | base64_encode("urlsafe") }}"
}
```

## Upload as base64 string

Some Connectors expect files as base64 strings. In order to upload files like this, the [#upload](#upload "mention") parameter needs to be used.

So the Action setup would look something like this:

{% code title="UI Form Schema" %}

```json
{
  "request_binary_url": {
    "title": "File URL where the file is fetched from",
    "type": "text",
    "required": false,
    "upload": false,
    "info": "The file can come from a previous step of a Locoia flow or from a static location on the web"
  }
}
```

{% endcode %}

{% code title="Request Body Template" %}

```django
{
  "some_field_name": "{{ binary_file.content | base64_encode }}"
}
```

{% endcode %}

## Uploading files using REST Helper

You can also upload a file using REST Helper.&#x20;

1. **Specify the API URL:**
   * Determine the appropriate API endpoint you want to interact with.
2. **Set Authorization Header:**
   * Optional, if required by the API
   * [More details](https://docs.locoia.com/connectors/building-connectors/pages/-Mg6fYnEj2YFVTXckIY_#2.-add-your-connector-auth)
3. **Select Content Type:**

   * If you want to upload a file, you should select

   `multipart/form-data`&#x20;
4. **Set empty Body**

   `{}`
5. **File to Upload**&#x20;

   `{{ some_pdf }}`

<figure><img src="/files/6Nj8Nx3VAyWHSHJWJN6j" alt=""><figcaption></figcaption></figure>


---

# 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.locoia.com/connectors/building-connectors/uploading-files-in-actions.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.
