Script question

I am trying to write a srcipt to call an API that returns a json transcript.

let audio_url = 'http://abcd.wav';
let language_behaviour = 'automatic multiple languages';


let form = new FormData();
form.append('audio_url', audio_url);

Gives me this error:

Script Error: ReferenceError: Can't find variable: FormData
Line number: 4, Column 24

Appreciate any advice to fix this

That error is telling you that FormData doesn’t exist. while it is something supported in browsers, it isn’t something available to the JavaScript engine Drafts is utilising.

Drafts provides an HTTP class to interact with web API.

Thank you for pointing me in the right direction!

Here’s the API instruction that I am tying to incorporate

curl -X 'POST' \
    'https://api.abc' \
    -H 'accept: application/json' \
    -H 'x-abc-key: 123456' \
    -H 'Content-Type: multipart/form-data' \
    -F "audio_url=http://abcd.wav" \
    -F "language_behaviour=automatic multiple languages"

This is what I have:

const key = "1234656"
const language_behaviour = "automatic multiple languages"

// take draft contents as the input for audio URL
let audio_url = draft.processTemplate("[[draft]]")

let http = HTTP.create();
var response = http.request({
    "url": "https://api.abc",
    "method": "POST",
    "data": {
		"x-abc-key": key,
		"audio_url": audio_url,
		"language_behaviour": language_behaviour
    }
});

if (!response.success) {
    console.log(response.statusCode);
    console.log(response.error);
    context.fail()
}

I get the following error:
500

undefined
Script step completed.

Also, and if possible, can you let me know how to append the json output to the draft?

That is typically a server error. But you should check the docs for the API you are using.

I don’t know what the undefined relates to exactly, but something is not defined when it is called for use. It may relate to the 500 error. It may not. I can’t see from your info where in the script that occurs.

Using the append method for the draft is probably the easiest.

1 Like