cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

ContentLoaderFunctions in ThingWorx

100% helpful (2/2)
  • 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
      • Note: When declaring namespace URLs in an element make sure that there is a white space in between each declaration
Comments

Hi Experts,

     Just a quick help.

     I'm sending a POST request from TW to external SAP system gateway service.

    It failed due to CSRF token authentication.

   The solution is to send a GET request to the odata service and persist the XCSRF token value in a variable and pass it to the POST request header. Can someone tell a simple solution or the code snippet structure.

  Appreciate your help.

Hi Ankit,

 

LoadXML throws below error whenever try to execute it.

 

Message :: The markup in the document following the root element must be well-formed. - See Script Error Log for more details.

 

Below is my snippet.

 

var params = {
proxyScheme: undefined /* STRING */,
headers: {'userName':'testUser','token':'f7bbddf5-b139-4981-bf62-5fc2b3e5dfb2','classDate':'2019-01-25 14:12:20'} /* JSON */,
ignoreSSLErrors: undefined /* BOOLEAN */,
useNTLM: undefined /* BOOLEAN */,
workstation: undefined /* STRING */,
useProxy: undefined /* BOOLEAN */,
withCookies: undefined /* BOOLEAN */,
proxyHost: undefined /* STRING */,
url: 'http://10.10.268.35:8080/MyserviceWF/api/classLogin' /* STRING */,
timeout: undefined /* NUMBER */,
proxyPort: undefined /* INTEGER */,
password: undefined /* STRING */,
domain: undefined /* STRING */,
username: undefined /* STRING */
};

// result: XML
var result = Resources["ContentLoaderFunctions"].LoadXML(params);

 

Please correct,if required.

There issue appears to be not with your code but with the response your got that is not properly formatted XML document that is expected and can't be parsed. You may want to try using LoadText instead of LoadXML and examine the response to see if it is XML formatted or possibly in JSON or some other format.

 

You can find more information about ContentLoader class used in ContentLoaderFunctions in JavaDoc at http://support.ptc.com/help/thingworx_hc/javadoc/com/thingworx/resources/content/ContentLoader.html

Version history
Last update:
‎Mar 03, 2017 12:24 AM
Updated by:
Labels (1)
Tags (1)