ContentLoaderFunctions in ThingWorx
- Want to do a REST call from ThingWorx
- Want to use REST to send request to External System.
- Want to get data from other system using REST
Here is how you can do this....
ThingWorx has ContentLoaderFunctions API which provides services to load or post content to and from other web applications. One can issue an HTTP request using any of the allowed actions (GET, POST, PUT, DELETE).
List of available ContentLoaderFunctions:
Delete
GetCookies
GetJSON
GetText
GetXML
LoadBinary
LoadImage
LoadJSON
LoadMediaEntity
LoadText
LoadXML
PostBinary
PostImage
PostJSON
PostMultipart
PostText
PostXML
PutBinary
PutJSON
PutText
PutXML
Example:
- Using LoadXML snippet in a custom service to retrieve an XML document from a specific URL
- Insert the LoadXML snippet into a custom service
var params = {
- proxyScheme: undefined /* STRING */,
- headers: "{ 'header1':'value1','header2':'value2'}" /* JSON */,
- ignoreSSLErrors: false /* BOOLEAN */,
- useProxy: undefined /* BOOLEAN */,
- proxyHost: undefined /* STRING */,
- url: "http://some_url/sampleXMLDocument.xml" /* STRING */,
- timeout: 30000 /* NUMBER */,
- proxyPort: undefined /* INTEGER */,
- password: "fakePassword" /* STRING */,
- username: "Administrator"/* STRING */
- };
- var result = Resources["ContentLoaderFunctions"].LoadXML(params);
- The snippet above contains an example of how to format any headers in JSON that need to be passed in, the URL that points directly to some XML document, a password, username, timeout, and ignoreSSLErrors set to false
- When LoadXML is exercised it will retrieve the XML document, and this can then be parsed or handled however is necessary
- To see the XML document that is returned from this service the service can be called from a third-party client, such as Postman
- Note: If a proxy or username and password are required to connect to the URL, those parameter MUST be specified
- Using the PostXML snippet in a custom service to send content to another URL, in this example, another service in Composer
- Insert the PostXML snippet into a custom service
var content = "<xml><tag1>NAME</tag1><tag2>AGE</tag2></xml>";
var params = {
url: "http://localhost/Thingworx/Things/thingName/Services/serviceName?postParameter=parameterName" /* STRING */,
content: content /* STRING */,
password: "admin" /* STRING */,
username: "Administrator" /* STRING */
};
var result = Resources["ContentLoaderFunctions"].PostXML(params);
- When posting XML content to another ThingWorx service the postParameter header must be defined in the url parameter for the PostXML snippet
- The postParameter header, in the url parameter, is set equal to the name of the input parameter for the service we are POSTing to
- Change the parameterName variable in the url to the name of the input parameter defined for the service
- The content parameter is set to the XML content that will be passed into the function or manually specified
- The postParameter header, in the url parameter, is set equal to the name of the input parameter for the service we are POSTing to
- Note: When declaring namespace URLs in an element make sure that there is a white space in between each declaration
- ex: <root xmlns:h="http://www.w3.org/TR/html4/" xmlns:f="http://www.w3schools.com/furniture">
- When posting XML content to another ThingWorx service the postParameter header must be defined in the url parameter for the PostXML snippet

