The N/HTTPS module allows you to send HTTP requests to external endpoints (e.g., third-party APIs) and handle the responses. It supports both synchronous and asynchronous calls.
Key Features of N/HTTPS
- HTTP Methods: Supports standard HTTP methods such as
GET,POST,PUT, andDELETE. - Authentication: Allows for various authentication methods, such as basic authentication or OAuth, depending on the external service’s requirements.
- Request Headers: You can specify custom headers for the request, such as content type or authorization tokens.
- Timeout Handling: Allows you to set timeouts for your requests to ensure they do not hang indefinitely.
- Handling Responses: You can handle different types of responses from the external service, including checking for successful status codes and parsing response data.
Key Methods in N/HTTPS
Here are the key methods available in the N/HTTPS module:
- get(): Makes a GET request to an external service. Used for retrieving data from an external API.
- post(): Makes a POST request to an external service. Used for sending data to an external API.
- put(): Makes a PUT request to update an existing resource on an external service.
- delete(): Makes a DELETE request to remove a resource from an external service.
- request(options): A more general method that allows you to make custom HTTP requests (can be used for GET, POST, PUT, DELETE, etc.).
Example: Making a GET Request
Here’s an example of making a GET request using N/HTTPS to fetch data from an external API:
define(['N/https', 'N/log'], function(https, log) {
function fetchData() {
var url = 'https://api.example.com/data'; // External API URL
var response = https.get({
url: url
});
if (response.status === 200) {
log.debug('Response Data', response.body); // Log the response body
} else {
log.error('Error', 'Failed to fetch data: ' + response.status);
}
}
return {
execute: fetchData
};
});
In this example, a GET request is made to https://api.example.com/data. If the request is successful (status code 200), the response body is logged.
Example: Making a POST Request
Here’s an example of making a POST request using N/HTTPS to send data to an external service:
define(['N/https', 'N/log'], function(https, log) {
function sendData() {
var url = 'https://api.example.com/submit'; // External API URL
var payload = {
name: 'John Doe',
email: 'john.doe@example.com'
};
var headers = {
'Content-Type': 'application/json' // Specify the content type as JSON
};
var response = https.post({
url: url,
headers: headers,
body: JSON.stringify(payload)
});
if (response.status === 200) {
log.debug('Response Data', response.body); // Log the response body
} else {
log.error('Error', 'Failed to send data: ' + response.status);
}
}
return {
execute: sendData
};
});
In this example, a POST request is made to https://api.example.com/submit with a JSON payload containing user data.
Handling Errors and Timeouts
It’s important to handle errors when making HTTP requests to external services, especially if the external service is down or responding slowly. You can also set timeouts to ensure that the script doesn’t hang forever if the external service takes too long to respond.
Here’s how you can handle timeouts and errors:
define(['N/https', 'N/log'], function(https, log) {
function fetchDataWithTimeout() {
var url = 'https://api.example.com/data'; // External API URL
try {
var response = https.get({
url: url,
timeout: 10000 // Set a 10-second timeout
});
if (response.status === 200) {
log.debug('Response Data', response.body); // Log the response body
} else {
log.error('Error', 'Failed to fetch data: ' + response.status);
}
} catch (error) {
log.error('Error', 'Request failed: ' + error.message);
}
}
return {
execute: fetchDataWithTimeout
};
});
In this example, a timeout of 10 seconds is set for the request. If the request takes longer than that, it will throw an error, and we log that error in the catch block.
Authentication with N/HTTPS
For requests to external services that require authentication, you can provide authentication details in the headers.
Example with Basic Authentication:
define(['N/https', 'N/log'], function(https, log) {
function fetchDataWithAuth() {
var url = 'https://api.example.com/data'; // External API URL
var headers = {
'Authorization': 'Basic ' + encodeBase64('username:password') // Base64 encoded username:password
};
var response = https.get({
url: url,
headers: headers
});
if (response.status === 200) {
log.debug('Response Data', response.body);
} else {
log.error('Error', 'Failed to fetch data: ' + response.status);
}
}
return {
execute: fetchDataWithAuth
};
});
In this example, Basic Authentication is used to authenticate the request by passing a base64-encoded string of username:password in the Authorization header.
Performance Considerations
- Request Frequency: External API calls consume resources and can be slow, so avoid making unnecessary requests.
- Error Handling: Always handle timeouts and errors gracefully to ensure your script doesn’t fail unexpectedly.
- Rate Limits: Be aware of any rate limits imposed by the external service to avoid being blocked for making too many requests in a short period.
Conclusion
The N/HTTPS module is a powerful tool for integrating NetSuite with external web services via HTTP. Whether you’re fetching data from a REST API, posting information to an external system, or handling authentication, this module gives you the ability to interact with external resources seamlessly from SuiteScript.