<h2>RESTAPIResponse - setHeader(String header, String value)</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="concept" /><meta name="DC.title" content="RESTAPIResponse - Scoped, Global" /><meta name="abstract" content="The RESTAPIResponse API allows you to build a RESTful response to a scripted REST API request." /><meta name="description" content="The RESTAPIResponse API allows you to build a RESTful response to a scripted REST API request." /><meta name="DC.subject" content="RESTAPIResponse, Scripted REST APIs, getStreamWriter(), setBody(Object body), setContentType(String contentType), setError(Object error), setHeader(String header, String value), setHeaders(Object headers), setLocation(String location), setStatus(Number status)" /><meta name="keywords" content="RESTAPIResponse, Scripted REST APIs, getStreamWriter(), setBody(Object body), setContentType(String contentType), setError(Object error), setHeader(String header, String value), setHeaders(Object headers), setLocation(String location), setStatus(Number status)" /><meta name="DC.relation" scheme="URI" content="../../../../../build/applications/concept/api-server.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-reference.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="c_ScriptableServiceResponseBuilder" /><link rel="stylesheet" type="text/css" href="../../../../../CSS/commonltr.css" /><title>RESTAPIResponse - Scoped, Global</title></head><body> <div class="nested0" id="c_ScriptableServiceResponseBuilder"> <h1 class="title topictitle1" id="ariaid-title1">RESTAPIResponse - Scoped, Global</h1> <div class="body conbody"><p class="shortdesc">The <span class="keyword apiname">RESTAPIResponse</span> API allows you to build a RESTful response to a scripted REST API request.</p> <p class="p">This API runs in the <code class="ph codeph">sn_ws</code> namespace.</p> <div class="p"><div class="note"><span class="notetitle">Note:</span> You cannot instantiate objects of this type. Objects of this type are created automatically and are accessible only in scripted REST API resource scripts.</div> </div> </div> <div class="related-links"> <div class="familylinks"> <div class="parentlink"><strong>Parent Topic:</strong> <a class="link" href="../../../../../build/applications/concept/api-server.html" title="Use server APIs in scripts to change the functionality of applications, or when you create new applications.">Server API reference</a></div> </div> </div><div class="topic reference nested1 api-method" id="r_SSRB-getStreamWriter"> <h2 class="title topictitle2" id="ariaid-title2">RESTAPIResponse - getStreamWriter()</h2> <div class="body refbody"><p class="shortdesc">Returns the ResponseStreamWriter for this response, allowing you to write directly to the response stream.</p> <div class="section"> <p class="p">Set the content type and status code using the <span class="keyword apiname">setHeaders</span> and <span class="keyword apiname">setStatus</span> functions prior to calling the <span class="keyword apiname">getStreamWriter</span> function.</p> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_SSRB-getStreamWriter__table_wnn_glb_sr" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 1. </span>Parameters</span></caption><colgroup><col /><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e175">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e178">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e181">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e175 ">None</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e178 "> </td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e181 "> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_SSRB-getStreamWriter__table_xnn_glb_sr" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 2. </span>Returns</span></caption><colgroup><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e216">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e219">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e216 ">RESTAPIResponseStream - Scoped, Global</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e219 ">The ResponseStreamWriter for this response. You can use this object to write directly to the response stream.</td></tr></tbody></table> </div> </div> <div class="example"> <pre class="pre codeblock"><code>response.setContentType('application/json'); response.setStatus(200); var writer = response.getStreamWriter();</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="r_SSRB-setBody_Object"> <h2 class="title topictitle2" id="ariaid-title3">RESTAPIResponse - setBody(Object body)</h2> <div class="body refbody"><p class="shortdesc">Sets the body content to send in the web service response.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_SSRB-setBody_Object__table_yzt_cmr_qr" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 3. </span>Parameters</span></caption><colgroup><col /><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e305">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e308">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e311">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e305 ">body</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e308 ">Object</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e311 "><p class="p">The response body, as a JavaScript object.</p> <p class="p">The body content is automatically serialized to JSON or XML depending on the value of the <span class="keyword parmname">Accept</span> header passed in the request.</p> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_SSRB-setBody_Object__table_zzt_cmr_qr" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 4. </span>Returns</span></caption><colgroup><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e355">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e358">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e355 ">void</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e358 "> </td></tr></tbody></table> </div> </div> <div class="example"> <pre class="pre codeblock"><code>var body = {}; body.name = "incident"; body.number = "1234"; body.caller = {"id": "user1"}; response.setBody(body); </code></pre> </div> <div class="example"> <pre class="pre codeblock"><code>var bodyArray = []; var body = {}; body.name = "incident"; body.number = "1234"; body.caller = {"id":"user1"}; bodyArray.push(body); response.setBody(bodyArray);</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="r_SSRB-setContentType_String"> <h2 class="title topictitle2" id="ariaid-title4">RESTAPIResponse - setContentType(String contentType)</h2> <div class="body refbody"><p class="shortdesc">Assigns a value to the Content-Type header in the web service response.</p> <div class="section"> <p class="p">You must set a response content type before writing the response. The content type is set automatically for string responses, based on the request Accept header value.</p> <p class="p">Setting an invalid content type causes the response to default to JSON. Failing to set a content type results in a status code 500 error when sending a binary response.</p> <p class="p">See the <a class="xref" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17" target="_blank" rel="noopener noreferrer">W3 Content-Type header documentation</a> for more information about this header.</p> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_SSRB-setContentType_String__table_dcn_3lr_qr" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 5. </span>Parameters</span></caption><colgroup><col /><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e463">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e466">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e469">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e463 ">contentType</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e466 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e469 ">The content type of the response body, such as application/json.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_SSRB-setContentType_String__table_ecn_3lr_qr" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 6. </span>Returns</span></caption><colgroup><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e506">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e509">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e506 ">void</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e509 "> </td></tr></tbody></table> </div> </div> <div class="example"> <pre class="pre codeblock"><code>response.setContentType('application/json');</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="r_SSRB-setError_O"> <h2 class="title topictitle2" id="ariaid-title5">RESTAPIResponse - setError(Object error)</h2> <div class="body refbody"><p class="shortdesc">Configures the REST response to return an error.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_SSRB-setError_O__table_apv_hb3_zs" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 7. </span>Parameters</span></caption><colgroup><col /><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e594">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e597">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e600">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e594 ">error</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e597 ">Object</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e600 ">Error object. <p class="p">For more information about the types of error objects that can be used, see <a class="xref" href="../../../../../integrate/custom-web-services/reference/r_ScriptedRESTServiceScriptExamples.html" title="These examples demonstrate how to create various resource scripts for a scripted REST API.">Scripted REST API example - script samples</a>.</p> </td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_SSRB-setError_O__table_bpv_hb3_zs" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 8. </span>Returns</span></caption><colgroup><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e646">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e649">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e646 ">void</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e649 "> </td></tr></tbody></table> </div> </div> <div class="example"> <p class="p">The following example shows how to return an error from within a scripted REST resource.</p> <pre class="pre codeblock"><code>var queryParams = request.queryParams; var userId = String(queryParams.user_id || ''); var fileId = String(queryParams.file_id || ''); if (!userId || !fileId){ response.setError(new sn_ws_err.BadRequestError('Missing required parameters.')); return; }</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="r_SSRB-setHeader_String_String"> <h2 class="title topictitle2" id="ariaid-title6">RESTAPIResponse - setHeader(String header, String value)</h2> <div class="body refbody"><p class="shortdesc">Assign a value to a REST service response header.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_SSRB-setHeader_String_String__table_und_rt4_gs" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 9. </span>Parameters</span></caption><colgroup><col /><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e738">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e741">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e744">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e738 ">header</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e741 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e744 ">The header you want to set.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e738 ">value</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e741 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e744 ">The value to assign the specified header.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_SSRB-setHeader_String_String__table_vnd_rt4_gs" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 10. </span>Returns</span></caption><colgroup><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e793">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e796">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e793 ">void</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e796 "> </td></tr></tbody></table> </div> </div> <div class="example"> <pre class="pre codeblock"><code>response.setHeader("Location","<URI>");</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="r_SSRB-setHeaders_Object"> <h2 class="title topictitle2" id="ariaid-title7">RESTAPIResponse - setHeaders(Object headers)</h2> <div class="body refbody"><p class="shortdesc">Sets the headers for the web service response.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_SSRB-setHeaders_Object__table_slb_j3r_qr" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 11. </span>Parameters</span></caption><colgroup><col /><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e881">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e884">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e887">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e881 ">headers</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e884 ">Object</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e887 ">A JavaScript object listing each header and the value to assign that header.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_SSRB-setHeaders_Object__table_tlb_j3r_qr" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 12. </span>Returns</span></caption><colgroup><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e924">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e927">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e924 ">void</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e927 "> </td></tr></tbody></table> </div> </div> <div class="example"> <pre class="pre codeblock"><code>var headers = {}; headers.X-Total-Count=100; headers.Location='https://instance.service-now.com/<endpoint_to_resource>'; response.setHeaders(headers);</code></pre> </div> </div> </div> <div class="topic reference nested1 api-method" id="r_SSRB-setLocation_String"> <h2 class="title topictitle2" id="ariaid-title8">RESTAPIResponse - setLocation(String location)</h2> <div class="body refbody"><p class="shortdesc">Assigns a value to the Location header in the web service response.</p> <div class="section"> <p class="p">See the <a class="xref" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30" target="_blank" rel="noopener noreferrer">W3 Location header documentation</a> for more information about this header.</p> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_SSRB-setLocation_String__table_mzd_1jr_qr" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 13. </span>Parameters</span></caption><colgroup><col /><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e1020">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e1023">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e1026">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e1020 ">None</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e1023 ">String</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e1026 ">An absolute URI to redirect the response recipient to.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_SSRB-setLocation_String__table_nzd_1jr_qr" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 14. </span>Returns</span></caption><colgroup><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e1063">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e1066">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e1063 ">void</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e1066 "> </td></tr></tbody></table> </div> </div> </div> </div> <div class="topic reference nested1 api-method" id="r_SSRB-setStatus_Number"> <h2 class="title topictitle2" id="ariaid-title9">RESTAPIResponse - setStatus(Number status)</h2> <div class="body refbody"><p class="shortdesc">Sets the status code number for the web service response.</p> <div class="section"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_SSRB-setStatus_Number__table_tj5_khr_qr" class="table parameters" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 15. </span>Parameters</span></caption><colgroup><col /><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e1145">Name</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e1148">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e1151">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e1145 ">status</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e1148 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e1151 ">The status code to send in the response, such as 200 to indicate success. Passing a non-numerical value, such as a string, causes the status code to default to 0.</td></tr></tbody></table> </div> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="r_SSRB-setStatus_Number__table_uj5_khr_qr" class="table returns" frame="border" border="1" rules="all"><caption><span class="tablecap"><span class="table--title-label">Table 16. </span>Returns</span></caption><colgroup><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e1188">Type</th><th class="entry cellrowborder" style="vertical-align:top;" id="d345283e1191">Description</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e1188 ">void</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d345283e1191 "> </td></tr></tbody></table> </div> </div> <div class="example"> <pre class="pre codeblock"><code>response.setStatus(200);</code></pre> </div> </div> </div> </div> </body></html></div>