HTTP POST Requests in SuiteScript without Authentication
Master HTTP POST requests in SuiteScript. Learn about syntax, parameters, errors, and best practices for effective scripting.
Starting in SuiteScript 2.x, developers can send HTTP POST requests using the http module, which facilitates communication between NetSuite and external servers. This feature is crucial for integrating third-party services and exchanging data seamlessly.
Method Description
The method sends an HTTP POST request as specified by the developer.
Important:
- Timeouts: Connections will timeout if it takes longer than 5 seconds to connect to the server. The request payload will time out after 45 seconds if data transfer exceeds this duration.
- Unauthorized Use: This method cannot function in unauthenticated client-side contexts. For further details, consult the SuiteAnswers article on outbound HTTPS nuances.
Returns
The method returns either a http.ClientResponse or an http.ServerResponse object, allowing you to handle responses appropriately.
Supported Script Types
- Client scripts
- Server scripts
Governance
This method consumes 10 governance units, which is important to consider for script performance and limits.
Module
Utilizes the N/http Module for implementation.
Parameters
Below are the parameters that can be passed to the POST request:
| Parameter | Type | Required / Optional | Description |
|---|---|---|---|
options.body | string | Required | The POST payload data. Note that sending files with multipart/form-data is not supported. |
options.url | string | Required | The complete HTTP/HTTPS URL to the destination. Refer to the N/url Module for URL handling. |
options.headers | Object | Optional | Custom HTTP headers for the request. |
Errors
Understanding possible error codes can help in debugging HTTP requests:
| Error Code | Message | Thrown If |
|---|---|---|
SSS_INVALID_HOST_CERT | An untrusted, privileged, or invalid certificate. | Security negotiation failure for the connection. |
SSS_INVALID_URL | The URL must be a fully qualified HTTP/HTTPS URL. | Given URL isn't properly formatted. |
SSS_MISSING_REQD_ARGUMENT | Missing a required argument. | If options.body or options.url is not provided. |
SSS_REQUEST_LOOP_DETECTED | A recursive function exceeds the allowed call limit. | If the script recursively calls itself via HTTP requests. |
Syntax Example
The following snippet illustrates how to set up an HTTP POST request in SuiteScript. Note that this is just a syntax template for educational purposes:
1// Sample code snippet for POST request 2var headerObj = {3 name: 'Accept-Language',4 value: 'en-us'5};6var response = http.post({7 url: 'http://www.testwebsite.com',8 body: 'My POST Data',9 headers: headerObj10});11 12var myresponse_body = response.body; // see http.ClientResponse.body13var myresponse_code = response.code; // see http.ClientResponse.code14var myresponse_headers = response.headers; // see http.Clientresponse.headersConclusion
Understanding how to utilize HTTP POST requests in SuiteScript is vital for developers needing to interact with external APIs securely and efficiently. Make sure to validate your parameters and handle potential errors appropriately to ensure smooth functionality.
Key Takeaways
- POST Request Use: Use the
http.postmethod in SuiteScript for sending data to external servers. - Error Handling: Pay attention to error codes for better debugging and resolve issues quickly.
- Governance Units: Keep the governance unit consumption in mind to optimize script performance.
Frequently Asked Questions (4)
Can HTTP POST requests be executed in client-side scripts without authentication in SuiteScript?
What happens if the connection to the server times out during an HTTP POST request in SuiteScript?
Are there any special considerations for the payload data type in SuiteScript HTTP POST requests?
How many governance units are consumed by sending an HTTP POST request using SuiteScript?
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.
