Edits history of script submission #22726 for ' Download Attachment (servicenow)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import * as wmill from "windmill-client"
    import { S3Object } from "windmill-client"
    
    function authHeader(auth: RT.Servicenow) {
      return auth.token
        ? `Bearer ${auth.token}`
        : `Basic ${btoa(`${auth.username}:${auth.password}`)}`
    }
    
    /**
     * Download Attachment
     * Download an attachment's binary content by its sys_id and write it to Windmill object storage, returning the S3 file reference.
     */
    export async function main(
      auth: RT.Servicenow,
      attachment_sys_id: string,
      output_s3_path: string
    ) {
      const response = await fetch(
        `${auth.instance_url}/api/now/attachment/${attachment_sys_id}/file`,
        {
          method: "GET",
          headers: {
            Authorization: authHeader(auth),
          },
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const output: S3Object = { s3: output_s3_path }
      await wmill.writeS3File(output, await response.blob())
      return output
    }
    

    Submitted by hugo989 5 days ago