Edits history of script submission #15910 for ' Download submission file (formstack)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Formstack = {
      token: string;
    };
    /**
     * /download/:submission_id/:field_id/:index
     * Download submitted file for a specific submission. Returns binary encoding of file.
     */
    export async function main(
      auth: Formstack,
      submission_id: string,
      field_id: string,
      index: string,
      encryption_password: string | undefined,
    ) {
      const url = new URL(
        `https://www.formstack.com/api/v2/download/${submission_id}/${field_id}/${index}`,
      );
      for (const [k, v] of [["encryption_password", encryption_password]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.text();
    }
    

    Submitted by hugo697 235 days ago