Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X
Hello, I'm currently trying to make a POSTXML request to an external server webhook which accepts XML payloads and parses them according to our requirements. I'm trying to use the ContentLoaderFunctions PostXML function but when I execute the service I get
[message: Execution error in service script [UptimeAlert] :: Premature end of file.]
My code is the following with our actual URL redacted for security reasons
// result: STRING var IPAddress = Things["UsefulServerInfo"].ServerName; var newUrlName = me.name.replace(" ", "%20"); var server = '<Server>'+IPAddress+'</Server>'; var site = '<Site>'+me.relatedSite+'</Site>'; var line = '<Line>'+me.relatedLines+'</Line>'; var asset = '<Asset>'+me.displayName+'</Asset>'; var technology = '<Technology>None</Technology>'; var alertname = '<AlertName>'+AlertName+'</AlertName>'; var alertdescription = '<AlertDescription>'+AlertDescription+'</AlertDescription>'; var timestamp = '<Timestamp>'+AlertTime+'</Timestamp>'; var url = '<URL>http://'+IPAddress+':9080/Thingworx/Runtime/index.html#master=PTC.SCA.SCO.AssetMonitor.MasterMashup&mashup=PTC.SCA.SCO.AssetMonitor.AssetDetailContainerMashup&defaultMashup=PTC.SCA.SCO.AssetMonitor.AssetDetail.PerformanceMonitorContainerMashup&assetName='+newUrlName+'&nestedMashup=undefined</URL>'; var stringtoxml = '<xml>' + server + site + line + asset + technology + alertname + alertdescription + timestamp + url + '</xml>'; var content = new XML(stringtoxml); var params = { proxyScheme: undefined /* STRING */, headers: undefined /* JSON */, ignoreSSLErrors: undefined /* BOOLEAN */, useNTLM: undefined /* BOOLEAN */, workstation: undefined /* STRING */, useProxy: undefined /* BOOLEAN */, withCookies: undefined /* BOOLEAN */, proxyHost: undefined /* STRING */, url: "https://redacted" /* STRING */, content: content /* XML */, timeout: undefined /* NUMBER */, proxyPort: undefined /* INTEGER */, password: undefined /* STRING */, domain: undefined /* STRING */, username: undefined /* STRING */ }; Resources["ContentLoaderFunctions"].PostXML(params);
The content ends up as an XML format below, with sensitive information again redacted, but the information is filled in as we are expecting and I have run a POST test with the same content to the same url with Postman and have no issue, I only experience an issue once I add the function call for the PostXML().
<xml> <Server>redacted</Server> <Site>redacted</Site> <Line>redacted</Line> <Asset>redacted</Asset> <Technology>Nothing</Technology> <AlertName>SomeName</AlertName> <AlertDescription>SomeDescription</AlertDescription> <Timestamp>Wed Feb 27 2019 13:41:00 GMT-0000 (UTC)</Timestamp> <URL>http://redacted:9080/Thingworx/Runtime/index.html#master=PTC.SCA.SCO.AssetMonitor.MasterMashup&mashup=PTC.SCA.SCO.AssetMonitor.AssetDetailContainerMashup&defaultMashup=PTC.SCA.SCO.AssetMonitor.AssetDetail.PerformanceMonitorContainerMashup&assetName=redacted&nestedMashup=undefined</URL> </xml>
I've tried to examine the script log but it is useless as it just repeats the same error as reported in the service execute screen with no further information showing up in debug log mode, I've also tried to observe the request in the chrome network inspector and all I see further is an error 500 from the Thingworx server. Any input on what could be causing this would be appreciated.
Thank you
Tyler
Solved! Go to Solution.
Because I hate open threads that have no answer that may or may not help a future user, here's what was discovered about this.
This issue was caused because the webhook being sent to was designed expecting an XML payload format however was replying with a JSON payload format, when Thingworx got this response in a format it wasn't expecting the function just broke...we changed our sent out payload to JSON type and utilized the postJSON function instead and it functioned as expected.
It would be nice to see these functions that are expecting a specific type of reply to do a type check when they first receive the package so it could provide more useful error checking than currently provided...but as it is all I can recommend to anyone using these functions and running into the same error is double check your back and forth with your webhook. I recommend testing the webhook with a program like postman and verifying your reply and sent types match.
Because I hate open threads that have no answer that may or may not help a future user, here's what was discovered about this.
This issue was caused because the webhook being sent to was designed expecting an XML payload format however was replying with a JSON payload format, when Thingworx got this response in a format it wasn't expecting the function just broke...we changed our sent out payload to JSON type and utilized the postJSON function instead and it functioned as expected.
It would be nice to see these functions that are expecting a specific type of reply to do a type check when they first receive the package so it could provide more useful error checking than currently provided...but as it is all I can recommend to anyone using these functions and running into the same error is double check your back and forth with your webhook. I recommend testing the webhook with a program like postman and verifying your reply and sent types match.