ServerResponse Object Members in SuiteScript

ServerResponse object members in SuiteScript control HTTP responses in server scripts, offering methods to manage headers, redirects, and caching.

·2 min read·View Oracle Docs

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:

MethodDescription
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

javascript
function onRequest(context) {
var response = context.response;
response.addHeader({ name: 'Custom-Header', value: 'HeaderValue' });
}

Redirecting a User

javascript
function onRequest(context) {
var response = context.response;
response.sendRedirect({ type: http.RedirectType.RECORD, id: recordId });
}

Writing a PDF Response

javascript
function onRequest(context) {
var response = context.response;
response.renderPdf({ templateId: 'customtemplateid' });
}

Read-Only Properties

The ServerResponse object exposes the following read-only property:

PropertyDescription
ServerResponse.headersReturns 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.

Source: This article is based on Oracle's official NetSuite documentation.

Key Takeaways

  • The ServerResponse object is essential for managing HTTP responses.
  • Make use of methods like sendRedirect and addHeader for dynamic responses.
  • Utilize the read-only headers property to inspect response details.

Frequently Asked Questions (4)

What module is the ServerResponse object part of in SuiteScript?
The ServerResponse object is part of the N/http module in SuiteScript.
How can I add a custom HTTP header using the ServerResponse object?
You can add a custom HTTP header by using the ServerResponse.addHeader(options) method, specifying the name and value of the header in the options.
Can the ServerResponse object render a PDF directly?
Yes, the ServerResponse object can render a PDF directly using the renderPdf(options) method, where you specify the templateId for the PDF.
Is the headers property of the ServerResponse object writable?
No, the headers property of the ServerResponse object is read-only and allows you to inspect the headers associated with the server response.
Source: ServerResponse Object Members Oracle NetSuite Help Center. This article was generated from official Oracle documentation and enriched with additional context and best practices.

Was this article helpful?

More in Integration

View all Integration articles →