ServerResponse Object Members in SuiteScript
ServerResponse object members in SuiteScript control HTTP responses in server scripts, offering methods to manage headers, redirects, and caching.
The ServerResponse object in SuiteScript provides various members that allow developers to manage HTTP responses effectively within server scripts. Understanding how to utilize these members is crucial for creating robust Suitelet and RESTlet functionalities.
What is the ServerResponse Object?
The ServerResponse object is part of the N/http module, which facilitates the management of responses sent from HTTP servers, such as Suitelets and RESTlets. This object allows you to manipulate headers, write content, and redirect users, enhancing the interaction with HTTP clients.
Key Methods of ServerResponse
The following methods are available for the ServerResponse object:
| Method | Description |
|---|---|
ServerResponse.addHeader(options) | Adds a header to the response. |
ServerResponse.getHeader(options) | Returns the value of a response header. |
ServerResponse.renderPdf(options) | Generates and renders a PDF directly to the response. |
ServerResponse.sendRedirect(options) | Sets the redirect URL to a NetSuite resource. |
ServerResponse.setCdnCacheable(options) | Configures CDN caching for a specified duration. |
ServerResponse.setHeader(options) | Sets the value of a specific response header. |
ServerResponse.write(options) | Writes content (text, XML, HTML) to the response. |
ServerResponse.writeFile(options) | Writes a file to the response. |
ServerResponse.writeLine(options) | Writes line information (text, XML, HTML) to the response. |
ServerResponse.writePage(options) | Generates a complete page response. |
Example Usage of ServerResponse Methods
Here are some examples of how to use methods from the ServerResponse object:
Adding a Header
function onRequest(context) { var response = context.response; response.addHeader({ name: 'Custom-Header', value: 'HeaderValue' });}Redirecting a User
function onRequest(context) { var response = context.response; response.sendRedirect({ type: http.RedirectType.RECORD, id: recordId });}Writing a PDF Response
function onRequest(context) { var response = context.response; response.renderPdf({ templateId: 'customtemplateid' });}Read-Only Properties
The ServerResponse object exposes the following read-only property:
| Property | Description |
|---|---|
ServerResponse.headers | Returns the headers associated with the server response. |
Conclusion
Understanding the functionality provided by the ServerResponse object is vital for any SuiteScript developer working on server-side scripting. The ability to control headers, manage redirects, and render various content formats enriches the developer experience and user interaction.
Key insights gained from effectively using the ServerResponse can be crucial in ensuring optimal performance and usability of SuiteScripts in NetSuite environment.
Key Takeaways
- The
ServerResponseobject is essential for managing HTTP responses. - Make use of methods like
sendRedirectandaddHeaderfor dynamic responses. - Utilize the read-only
headersproperty to inspect response details.
Frequently Asked Questions (4)
What module is the ServerResponse object part of in SuiteScript?
How can I add a custom HTTP header using the ServerResponse object?
Can the ServerResponse object render a PDF directly?
Is the headers property of the ServerResponse object writable?
Was this article helpful?
More in SuiteScript
- SuiteScript 2.1 Enhancements in NetSuite February Updates
SuiteScript 2.1 now supports async features and PATCH method. Discover the latest API and SuiteProcurement improvements.
- Scheduling Map/Reduce Script Deployments in NetSuite
Learn to schedule map/reduce script submissions, including one-time and recurring options in NetSuite.
- Binary File Support in N/https Module for SuiteScript
SuiteScript enhances capabilities with binary file support in the N/https module, allowing improved data handling in external communications.
- API Governance Units Calculation in NetSuite 2026.1
NetSuite 2026.1 introduces examples illustrating API governance unit calculations for both user event and scheduled scripts.
