Scripted REST API の例 - 添付ファイルのストリーミング

  • リリースバージョン: Zurich
  • 更新日 2025年07月31日
  • 所要時間:1分
  • この例では、画像添付ファイルをバイナリストリームとして要求ユーザーに送信する方法を示します。

    /**
     *  Sample Scripted REST Resource that returns a stream of binary representing an attachment
     *  This sample uses ServiceNow JavaScript API GlideSysAttachmentInputStream to get an attachment as a stream then
     *  users WriteStream to stream the response.
     */
    (function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
        var hdrs = {},
            attachment_sys_id = '1852fd52471321009db4b5b08b9a71a9';
    
        hdrs['Content-Type'] = 'image/jpeg';
        response.setStatus(200);
        response.setHeaders(hdrs);
    
        var writer = response.getStreamWriter();
        var attachmentStream = new GlideSysAttachmentInputStream(attachment_sys_id);
        writer.writeStream(attachmentStream);
    })(request, response);

    このリソースへの要求は、次の応答を返します。

    //  sample response
    /*
     HTTP/1.1 200 OK
     Set-Cookie: glide_session_store=SYSTEM; Expires=Fri, 30-Oct-2015 21:57:00 GMT; Path=/; HttpOnly
     Content-Type: image/jpeg
     Transfer-Encoding: chunked
     Date: Fri, 30 Oct 2015 21:26:59 GMT
     Connection: close
     Server: ServiceNow
    
     <binary response body excluded from this sample>
     */