Bulk-create txt files with Dropbox API

I’m trying to create a complex script that reads from an airtable database, creates a text based on a markdown template replacing placeholders with info from airtable, saves textfiles to dropbox and triggers a shortcut to create PDFs from the text files. I pretty much have a working solution for each part of the script using iCloud, but since Apple won’t let me write or retrieve files from other than the app-specific folder, it’s hard to integrate the drafts action and the shortcut. Because of this limitation I’m now trying to write to Dropbox instead.

Help me create a file upload api request to dropbox

I’m in a little over my head trying to create api calls from scratch, but I want to learn. I have read the dropbox api documentation, but I’m struggling to understand it and translate it to a Drafts api request.

This is what I have so far:

//Create Dropbox object
let dbxMain = Dropbox.create();

//Build some file info
let txt = `#Testfile
This is a testfile created by Drafts.
- Ain't that swell?`;

let path = "/0-temp/test/testfile.txt";

//Build the arguments for calling the Dropbox move
let urlEndpoint = "https://content.dropboxapi.com/2/files/upload";

let dictArgs = {
    "path": path,
    "mode": "add",
    "autorename": true,
    "mute": false,
    "strict_conflict": false
};

//Create the file

let objResp = dbxMain.contentUploadRequest(
  {
	  "url": urlEndpoint,
	  "method": "POST",
	  "parameters": dictArgs,
	  "data": txt
  });

This gives me the following error:

I read in the Dropbox API to include the content of the file in the body of the request, which interpreted as using the data property of the DropboxRequestSettings argument in the API call. From what I understand of this error message, I’m either sending the file content (string) in the wrong way/format, or I’m failing to specify the format through a header.

Questions

  1. Am I understanding the error message correctly?
  2. Can I send the file content as a string, like I tried?
  3. Do I need to encode the string in any way, and how?
  4. Do I need to provide additional headers?

Any tips, advice or examples would be greatly appreciated!

You are close. The difference is that you are passing parameters that should be passed as dropbox-api-args. Try making the call like the below.

let objResp = dbxMain.contentUploadRequest(
  {
	  "url": urlEndpoint,
	  "method": "POST",
	  "dropbox-api-args": dictArgs,
	  "data": txt
  });
2 Likes

Thank you again! It works like a charm!