Skip to content

Instantly share code, notes, and snippets.

@RenatoExpert
Last active February 9, 2024 21:55
Show Gist options
  • Save RenatoExpert/3fa1a15f3bb56974e11f92533ddd44da to your computer and use it in GitHub Desktop.
Save RenatoExpert/3fa1a15f3bb56974e11f92533ddd44da to your computer and use it in GitHub Desktop.
/*
* This is a sample funtional code to send HTTP POST requests on Scada-LTS.
*
* It is sending an whatsapp message through an API service.
* Whatsapp service source: https://github.com/chrishubert/whatsapp-api
*
* Scada-LTS scripts are interpreted by jsdoc-toolkit package which runs Rhino at its core.
* Rhino has some limitations about javascript resources, as example: DOM API, Fetch API and XMLHttpRequest.
*/
var imports = JavaImporter(
org.apache.commons.httpclient.HttpClient,
org.apache.commons.httpclient.methods.PostMethod,
org.apache.commons.httpclient.methods.StringRequestEntity
);
var url = "http://127.0.0.1:3000/client/sendMessage/f8377d8d-a589-4242-9ba6-9486a04ef80c";
var payload = {
"chatId": "5517996161861@c.us",
"contentType": "string",
"content": "Hello World Scada-LTS!"
};
var body = JSON.stringify(payload);
with (imports) {
var client = new HttpClient();
var postMethod = new PostMethod(url);
postMethod.setRequestHeader("Accept", "/");
postMethod.setRequestHeader("x-api-key", "renato");
postMethod.setRequestHeader("Content-Type", "application/json");
postMethod.setRequestEntity(new StringRequestEntity(body, "application/json", "UTF-8"));
try {
var statusCode = client.executeMethod(postMethod);
if (statusCode === 200) {
var responseBody = postMethod.getResponseBodyAsString();
//print("Response body: " + responseBody);
} else {
// Handle other status codes
//print("Request failed with status code: " + statusCode);
}
} finally {
postMethod.releaseConnection();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment