Add HTTP Headers to Server Responses in SuiteScript

Learn to add custom HTTP headers to server responses using SuiteScript's HTTP module.

·2 min read·View Oracle Docs

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:

suitescript
1// Add additional code
2...
3serverResponse.addHeader({
4 name: 'Accept-Language',
5 value: 'en-us',
6});
7...
8// Add additional code

Method Signature

The method accepts an object with two required fields:

ParameterTypeDescription
options.namestringThe name of the HTTP header.
options.valuestringThe 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 the options.name or options.value parameters 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_HEADER errors.
  • 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:

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?
The `addHeader` method is available in Server scripts within NetSuite and is specifically used to handle HTTP responses.
What happens if I attempt to use an invalid header name with the `addHeader` method?
Using an invalid header name will trigger the `SSS_INVALID_HEADER` error. It's important to ensure both the header name and value are valid.
Are there any restrictions on the number of headers I can add using the `addHeader` method?
There are no governance limits on using the `addHeader` method, so you can add as many headers as needed for your HTTP responses.
What precautions should be taken when using custom header names with `addHeader`?
Custom header names should not contain underscores, as this may cause unexpected recognition issues. Always validate your headers before adding them.
Source: Syntax 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 →