N/http Module Script Samples
Explore N/http module script samples, including HTTP GET requests and redirecting records in SuiteScript 2.1.
The N/http module in SuiteScript provides developers with the ability to handle HTTP requests effectively. This article illustrates two practical examples of using the N/http module, including how to make GET requests and redirect records within the NetSuite environment.
How to Request a URL Using HTTP GET
In SuiteScript, you can perform an HTTP GET request to retrieve data from a specified URL. The following sample demonstrates how to execute a GET request using the N/http module:
Code Sample: HTTP GET Request
1/**2 * @NApiVersion 2.13 */4// This script uses an HTTP GET request for a URL.5require(['N/http'], (http) => {6 function sendGetRequest() {7 let response = http.get({8 url: 'http://www.google.com'9 });10 // Process the response11 log.debug({ title: 'Response', details: response.body });12 }13 sendGetRequest();14});Important Notes:
- Make sure to replace the URL with the desired endpoint you wish to request data from.
- The sample script is designed for use in the SuiteScript Debugger. For production deployment, adapt it appropriately based on the script type requirements.
How to Redirect a Record
Another useful feature of the N/http module is the ability to redirect users to a specific record within NetSuite. This example shows how to create a Suitelet that redirects to a new sales order record while setting an entity field.
Code Sample: Redirecting to a Sales Order Record
1/**2 * @NApiVersion 2.13 * @NScriptType Suitelet4 */5// This script redirects to a new sales order record and sets the entity.6define(['N/record', 'N/http'], (record, http) => {7 function onRequest(context) {8 context.response.sendRedirect({9 type: http.RedirectType.RECORD,10 identifier: record.Type.SALES_ORDER,11 parameters: ({12 entity: 6 // Ensure this is a valid entity ID13 })14 });15 }16 return {17 onRequest: onRequest18 };19});Important Notes:
- Ensure the
entityfield is populated with a valid internal ID from your NetSuite account to avoid errors during execution. - This sample script must be used in a deployed Suitelet environment.
Key Takeaways
- The N/http module facilitates HTTP communication within SuiteScript.
- Use the provided samples to handle GET requests and redirects effectively.
- Always validate IDs when passing parameters to avoid runtime errors.
Frequently Asked Questions (4)
Do I need special permissions to perform HTTP GET requests using the N/http module in SuiteScript?
Can I use the HTTP GET request code sample in a production environment without modification?
What is a crucial step when redirecting to a sales order record using the Suitelet sample?
Does the article mention a feature flag that must be enabled to use the N/http module?
Was this article helpful?
More in SuiteScript
- Common SuiteScript Errors and Solutions for NetSuite
Common NetSuite script errors include INVALID_SCRIPT_DEPLOYMENT_ID and SSS_AUTHORIZATION_HEADER_NOT_ALLOWED. Learn effective solutions.
- SuiteScript 2.x Sublist Values: Standard and Dynamic Mode
Use setSublistValue in standard mode; select a line, set its current value and commit it in dynamic mode. Choose the API from the record's mode.
- SuiteScript Record.setValue: Body Fields, Types and Errors
Set NetSuite body fields with Record.setValue, matching field IDs and value types and checking common API errors.
- SuiteScript 2.1 Enhancements and API Updates in NetSuite
SuiteScript 2.1 enables execution of 2.0 scripts and supports PATCH method for enhanced API capabilities.
