<h2>Scripted REST API example - streaming file attachments</h2><br/><div style="overflow-x:auto"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta content="text/html; charset=UTF-8" /><meta name="copyright" content="(C) Copyright 2025" /><meta name="DC.rights.owner" content="(C) Copyright 2025" /><meta name="generator" content="DITA-OT" /><meta name="DC.type" content="reference" /><meta name="DC.title" content="Scripted REST API example - streaming file attachments" /><meta name="abstract" content="This example demonstrates how to send an image attachment to a requesting user as a binary stream." /><meta name="description" content="This example demonstrates how to send an image attachment to a requesting user as a binary stream." /><meta name="DC.subject" content="Scripted REST APIs, streaming file attachments example" /><meta name="keywords" content="Scripted REST APIs, streaming file attachments example" /><meta name="DC.relation" scheme="URI" content="../../../integrate/custom-web-services/concept/c_ScriptedRESTAPIExamples.html" /><meta name="DC.relation" scheme="URI" content="../../../build/applications/concept/api-implementation-reference.html" /><meta name="DC.relation" scheme="URI" content="../../../build/applications/concept/api-implementation.html" /><meta name="DC.relation" scheme="URI" content="../../../integrate/web-services/reference/r_AvailableWebServices.html" /><meta name="DC.relation" scheme="URI" content="../../../integrate/inbound-rest/concept/c_RESTAPI.html" /><meta name="DC.relation" scheme="URI" content="../../../integrate/custom-web-services/concept/c_CustomWebServices.html" /><meta name="DC.creator" content="ServiceNow" /><meta name="DC.date.created" content="2023-08-03" /><meta name="DC.date.modified" content="2024-02-01" /><meta name="DC.format" content="XHTML" /><meta name="DC.identifier" content="r_ScriptedRESTExampleStreamAttach" /><link rel="stylesheet" type="text/css" href="../../../CSS/commonltr.css" /><title>Scripted REST API example - streaming file attachments</title></head><body id="r_ScriptedRESTExampleStreamAttach"> <div class="breadcrumb"><a class="link" href="../../../build/applications/concept/api-implementation-reference.html" title="Get started using ServiceNow APIs with guides, resources, and reference documentation.">API implementation and reference</a> > <a class="link" href="../../../build/applications/concept/api-implementation.html" title="You can use JavaScript APIs to extend application server and client functionality. Use web services to connect ServiceNow applications to other software applications.">API implementation</a> > <a class="link" href="../../../integrate/web-services/reference/r_AvailableWebServices.html" title="HTTP-based web services allow diverse applications to talk to each other. ServiceNow supports both inbound (provider) and outbound (consumer) web services.">Web services</a> > <a class="link" href="../../../integrate/inbound-rest/concept/c_RESTAPI.html" title="REST (REpresentational State Transfer) is a simple stateless architecture that provides standards between computer systems on the web, making it easier for them to communicate with each other.">REST APIs</a> > <a class="link" href="../../../integrate/custom-web-services/concept/c_CustomWebServices.html" title="The scripted REST API feature allows application developers to build custom web service APIs.">Scripted REST APIs</a> > </div> <h1 class="title topictitle1" id="ariaid-title1">Scripted REST API example - streaming file attachments</h1> <div class="body refbody"><p class="shortdesc">This example demonstrates how to send an image attachment to a requesting user as a binary stream.</p> <div class="example"> <pre class="pre codeblock"><code>/** * 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);</code></pre> <p class="p">A request to this resource returns the following response.</p> <pre class="pre codeblock"><code>// 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> */</code></pre> </div> </div> <div class="related-links"> <div class="familylinks"> <div class="parentlink"><strong>Parent Topic:</strong> <a class="link" href="../../../integrate/custom-web-services/concept/c_ScriptedRESTAPIExamples.html" title="Mutliple examples are available demonstrating how to create and use scripted REST APIs.">Scripted REST API examples</a></div> </div> </div></body></html></div>