When you need to send an HTTPS POST request synchronously in NetSuite, the https.post() method is the appropriate choice. This method is designed for scenarios where you require the script to wait for the completion of the HTTP request before proceeding.
Example:
let response = https.post({
url: url,
headers: { ‘Content-Type’: ‘application/json’ },
body: JSON.stringify({ key : value })
});
let responseBody = JSON.parse(response.body);
if (responseBody.success) {
//code to run for successful request
}
else{
//print the error
}
Key Features:
- Synchronous: The https.post() request unlike https.post.promise() method will wait for the response before moving on.
- Header: Supports headers like “Authorization” and “Content-Type”.
We need to ensure that a proper response is sent back.
Example:
scriptContext.response.write({
output: JSON.stringify({
success: true,
data: {
//data object
}
}),
contentType: ‘application/json’
});
This approach allows the Suitelet to send data back to the requesting script, with subsequent lines of code executing only after the response is received.