Help

Export Rich Text Field as a .md file to upload to Dropbox

Topic Labels: Automations
Solved
Jump to Solution
2512 5
cancel
Showing results for 
Search instead for 
Did you mean: 
Isabel_Vazquez
5 - Automation Enthusiast
5 - Automation Enthusiast

I’m trying to adapt the code by @Bill.French used here to upload a single long text field’s contents as a markdown document to Dropbox.

The automation is:
Trigger: When a new record is created
(records are created with nearly all fields filled at once, using Zapier, so the inputConfig fields are never empty)
Action: Run script…

// set the table name
let table = "Piece Information";

let inputConfig = input.config();
let fileContents = inputConfig.showNotes;
let fileName = inputConfig.fileID + "_" + inputConfig.fileName;
let fileNameExt = inputConfig.fileID + "_" + inputConfig.fileName + ".md";

// set the endpoint and app token
let dropboxEndpoint = "https://content.dropboxapi.com/2/files/upload";
let appToken = "<my app token>";

// set up the post options
let postOptions = {
    method: "post",
    headers: {
        "Authorization" : "Bearer " + appToken,
        "Dropbox-API-Arg" : "{\"path\": \"2021%20TC-RHDF%20Competition/ALL%20ENTRIES/"+fileNameExt+"\",\"mode\": \"add\",\"autorename\": true,\"mute\": false,\"strict_conflict\": false}",
        "Content-Type" : "application/octet-stream",
        "Accept" : "application/json",
    },
    body: fileContents
}
const postResults = await fetch(dropboxEndpoint, postOptions);
const jsonPost    = await postResults.json();

// display the dropbox upload result
output.markdown("Display Drop[box Response JSON Object");
output.inspect(jsonPost);

When I remove this section, I get no errors, but I don’t see a file uploaded in dropbox:

const postResults = await fetch(dropboxEndpoint, postOptions);
const jsonPost    = await postResults.json();

// display the dropbox upload result
output.markdown("Display Drop[box Response JSON Object");
output.inspect(jsonPost);

When I keep that section, I see this error:

SyntaxError: Unexpected token E in JSON at position 0 at main on line 40

Line 40 corresponds to: const jsonPost = await postResults.json();

Any sense what’s going wrong here?

I’m new to JavaScript and know almost nothing about JSON. I also have Zapier, on the off chance anyone knows an easier way to do this using Zapier (but it seems like the only dropbox options there are to upload a file with a .txt extension, and I need it to be a markdown file).

The reason for all of this is that I have a dropbox folder set up using JustCast to create a podcast RSS feed. Each audio file in that folder creates a podcast episode. Each audio file in that folder has a corresponding record in Airtable, with a “Summary” rich text field. Any markdown file placed into that folder with a matching file name to the audio file automatically becomes the Show Notes for the podcast episode.

Thanks for your help!!

1 Solution

Accepted Solutions
Isabel_Vazquez
5 - Automation Enthusiast
5 - Automation Enthusiast

Solved! The main issue was that I forgot to add the “/” before the file path. And none of the file path should be encoded. It’s working great now, many thanks!

Here’s the final script:

// set the table name
let table = "Piece Information";

// identify the field to be exported
let inputConfig = input.config();
let fileContents = inputConfig.showNotes;
let fileName = inputConfig.fileID + "_" + inputConfig.fileName;
let fileNameExt = inputConfig.fileID + "_" + inputConfig.fileName + ".md";


//display the contents of the file
//output.inspect(fileContents);

//
// write the file to a cloud drive service
//

// set the endpoint and app token
let dropboxEndpoint = "https://content.dropboxapi.com/2/files/upload";
let appToken = "<my app token>";

// set up the post options
let postOptions = {
    method: "post",
    headers: {
        "Authorization" : "Bearer " + appToken,
        "Dropbox-API-Arg" : "{\"path\": \"/2021 TC-RHDF Competition/ALL ENTRIES/"+ fileName +"/"+fileNameExt+"\",\"mode\": \"add\",\"autorename\": true,\"mute\": false,\"strict_conflict\": false}",
        "Content-Type" : "application/octet-stream",
        "Accept" : "application/json",
    },
    body: fileContents
}
const postResults = await fetch(dropboxEndpoint, postOptions);
const jsonPost = await postResults.text();

See Solution in Thread

5 Replies 5

Hi @Isabel_Vazquez and welcome to the community!

Yes, at that point it’s trying to parse what is expected to be a JSON object and it isn’t able to parse it. For debugging purposes, try changing it to:

const jsonPost = await postResults.text();

This will help you see what’s being sent in the response to the API call.

Isabel_Vazquez
5 - Automation Enthusiast
5 - Automation Enthusiast

Hi @Bill.French ! Thanks so much. I’ve been a lurker for a while, but noticed how active and helpful people are, so I thought I’d join the fun :slightly_smiling_face:

Setting aside for a moment that I’m getting the error message that neither ‘output.markdown’ nor ‘output.inspect’ are functions…

For debugging right now, I’m just running console.log(jsonPost) and I’m now seeing this error message:

"Error in call to API function "files/upload": HTTP header "Dropbox-API-Arg": path: '2021%20TC-RHDF%20Competition/ALL%20ENTRIES/6018C_You, Me and Pikachu.md' did not match pattern '(/(.|[\r\n])*)|(ns:[0-9]+(/.*)?)|(id:.*)'"

I thought the problem might be the filename not being encoded, but even when I adapted this line:
"Dropbox-API-Arg" : "{\"path\": \"2021%20TC-RHDF%20Competition/ALL%20ENTRIES/"+encodeURI(fileNameExt)+"\",\"mode\": \"add\",\"autorename\": true,\"mute\": false,\"strict_conflict\": false}",

I see the same error:

"Error in call to API function "files/upload": HTTP header "Dropbox-API-Arg": path: '2021%20TC-RHDF%20Competition/ALL%20ENTRIES/6018C_You,%20Me%20and%20Pikachu.md' did not match pattern '(/(.|[\r\n])*)|(ns:[0-9]+(/.*)?)|(id:.*)'"

Isabel_Vazquez
5 - Automation Enthusiast
5 - Automation Enthusiast

Solved! The main issue was that I forgot to add the “/” before the file path. And none of the file path should be encoded. It’s working great now, many thanks!

Here’s the final script:

// set the table name
let table = "Piece Information";

// identify the field to be exported
let inputConfig = input.config();
let fileContents = inputConfig.showNotes;
let fileName = inputConfig.fileID + "_" + inputConfig.fileName;
let fileNameExt = inputConfig.fileID + "_" + inputConfig.fileName + ".md";


//display the contents of the file
//output.inspect(fileContents);

//
// write the file to a cloud drive service
//

// set the endpoint and app token
let dropboxEndpoint = "https://content.dropboxapi.com/2/files/upload";
let appToken = "<my app token>";

// set up the post options
let postOptions = {
    method: "post",
    headers: {
        "Authorization" : "Bearer " + appToken,
        "Dropbox-API-Arg" : "{\"path\": \"/2021 TC-RHDF Competition/ALL ENTRIES/"+ fileName +"/"+fileNameExt+"\",\"mode\": \"add\",\"autorename\": true,\"mute\": false,\"strict_conflict\": false}",
        "Content-Type" : "application/octet-stream",
        "Accept" : "application/json",
    },
    body: fileContents
}
const postResults = await fetch(dropboxEndpoint, postOptions);
const jsonPost = await postResults.text();

Yep - apologies for this - I wasn’t fully on board with your question in the actions script despite you clearly indicating such. :winking_face:

Typically, I do not debug in actions so my code is rife with references to UI components.

No worries at all! I’m so new at this stuff, I’m not great at catching things that shouldn’t be there in different contexts.

Thanks for your help!