Add HTTP Headers to Server Responses in SuiteScript
Learn to add custom HTTP headers to server responses using SuiteScript's HTTP module. HTTP headers are fundamental to web communications, allowing additional
HTTP headers are fundamental to web communications, allowing additional data to be sent with HTTP requests and responses. This article covers how to use SuiteScript to add headers to server responses effectively.
What Is the addHeader Method?
The addHeader method is a function from the http.ServerResponse object used to add headers to the HTTP response. This method can be particularly useful for managing responses and ensuring that browsers handle them correctly according to your requirements. If a header already exists, this method adds a new line for that header, helping maintain flexibility in your message structuring.
Example Usage
To use the addHeader method, you will pass an object containing the header name and value as follows:
1// Add additional code 2...3serverResponse.addHeader({4 name: 'Accept-Language',5 value: 'en-us',6});7...8// Add additional codeMethod Signature
The method accepts an object with two required fields:
| Parameter | Type | Description |
|---|---|---|
options.name | string | The name of the HTTP header. |
options.value | string | The value of the HTTP header. |
Return Value
This method returns void, meaning it does not provide output but directly modifies the server response.
Error Handling
It's essential to handle errors when using the addHeader method. The following error codes may occur:
SSS_INVALID_HEADER: This error occurs if the header name or value is invalid.SSS_MISSING_REQD_ARGUMENT: This error is thrown if either theoptions.nameoroptions.valueparameters are missing.
Governance and Supported Types
The addHeader method is supported in Server scripts and does not impose any governance limits, allowing for more efficient handling of HTTP responses for your applications.
Best Practices
- Be cautious with custom header names: Ensure they do not contain underscores, as this can lead to unexpected issues with header recognition.
- Always validate header values to prevent
SSS_INVALID_HEADERerrors. - Review the blocked headers list and avoid attempting to set those values, as they will be ignored by the system.
Related Concepts
For more comprehensive information on HTTP headers and their usage, refer to the following topics:
- HTTP Header Information
- http.ServerResponse
- N/http Module
In conclusion, mastering the addHeader method of SuiteScript’s http.ServerResponse allows for enhanced manipulation of HTTP responses, thus ensuring successful communication with clients and appropriate content delivery.
Frequently Asked Questions (4)
Is the `addHeader` method available for all script types in NetSuite?
What happens if I attempt to use an invalid header name with the `addHeader` method?
Are there any restrictions on the number of headers I can add using the `addHeader` method?
What precautions should be taken when using custom header names with `addHeader`?
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.
