Can I issue a HTTP POST from Exosite Portals?


#1

Received this question from a customer:

I created an Google script that allows applications to issue an HTTP POST to a URL and it then writes the body of that POST into a Google spreadsheet.

Is there a way I can issue an HTTP POST from Exosite Portals to a URL that I specify? Is there a way to do it via Scripts or a custom Widget?


#2

I want to make sure I cover two options here - just to be sure I cover your request.

One Platform
One option is that you can use Lua Scripts or Dispatch resource in the One Platform. In a Lua script, you can do a HTTP POST or GET to any URL you specify, along with custom headers, etc.

Here are some examples of doing so in this repository:

You can also have a direct OneP dispatch resource that is subscribed to a dataport or datarule and will send any time that subscribed to resources gets a new value change.

Portals
If you truly want to send a message from a Portals widget - you can as long as that server implements CORS headers. You can make Javascript XMLHttpRequest requests.
Here is an example that calls Portals API using http request:


#3

A third option would be to do this entirely in Google Scripts, by issuing a POST request to the One Platform and then issuing a POST request with the data from the One Platform to the URL of your choice.

Here is a template for issuing a POST request to the One Platform in Google Scripts:

var payload = " {\"auth\": {\"cik\": \"DEVICECIKHERE\"}, \"calls\": [ { \"procedure\": \"read\",\"arguments\": [\"DATAPORTRIDHERE\", {}],  \"id\": 1 }]}";
  
var options = {
  "method"  : "POST",
  "payload" : payload,   
  "contentType": "application/json; charset=utf-8",
};

var result1 = UrlFetchApp.fetch("http://m2.exosite.com/onep:v1/rpc/process", options);
if (result1.getResponseCode() == 200) {
  Logger.log("successful POST");
}
else Logger.log("POST failed");

You can then call methods on result1 to obtain the data you’d like to POST to the other URL.