Files: uploads and downloads

Upload files as forms or raw bodies, receive PDFs and images, preview them in the log and keep them under names you choose.

Example flow

examples/Help/files.clem.json uploads a form with an image (files/avatar.png), downloads a PNG and saves it as images/sample-$userName.png, and reads a CSV file in code.

examples/Examples/20_response_viewers.clem.json shows each viewer of Response details: JSON, XML, HTML, an image and a large list.

The files folder

Each project has a files folder for the files its requests upload. Relative paths are read from there, so a flow keeps working on another computer and in exports. When you pick a file from elsewhere with Browse, CLEM offers to copy it in. Files Folder opens it. $vars work in paths: invoices/$orderId.pdf.

Uploads

In the request editor's Body tab, Send:

The Body tab in Multipart form mode with a text field and a file field
A multipart form with a text field and a file from the files folder.

Files in responses

A PDF, image, archive or any other binary response isn't shown as text. It is saved in the run's output folder (Settings → Output folder) and shown in the log as a file card with Preview, Open and Show in folder.

A multipart response (an echo of a form, a batch API) is shown as text: its fields as they are, and each file part as one line, such as <binary part "pic": image/png, 3.2 KB>. Body checks and extract rules work on that text.

The run log with the upload's checks and a file card for the downloaded image
The downloaded image as a file card in the log.

Previews: JSON as a collapsible tree, XML as a tree, CSV and TSV as a table, HTML and other text as source, images inline, PDFs in a viewer window. Text responses get the same viewers, with a Raw tab. Nothing is cut, however long.

Saving a response

On the Response tab, Save the response body to a file keeps any response under a name you choose, relative to the run's output folder: invoices/invoice-$id.pdf. Only when the status is 2xx skips error pages.

Files in code

HelperDoes
file(path, { fileName, contentType }?)A file to upload, for http().
readFile(path)A file from the files folder as text; readFile(path, null) as a Buffer.
saveFile(path, data)Writes a response, Buffer, text or JSON value to the output folder. The log shows it with a preview.
response.bufferThe raw bytes of the last response; also size, contentType, isBinary, fileName.
// Upload a file and a field (multipart)
const r = await http({ method: "POST", url: $api + "/upload", multipart: { title: "Q3", doc: file("reports/q3.pdf") } });

// One request per row of a CSV file in the files folder
for (const line of readFile("users.csv").trim().split("\n").slice(1)) {
  const [id] = line.split(",");
  await request("rq_user", { $id: id });
}

// Keep a downloaded file under your own name
saveFile("invoice-" + $orderId + ".pdf", response);

To run a whole flow once per CSV row, you don't need code: see Data-driven runs.

In exported flows

An export copies the files the flow uploads next to the script (<flow>.files, or <project>.files for a project). Saved responses go to ./clem-output/ unless --out-dir says otherwise; --save-binary keeps every binary response. Command-line options.