Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X
Hello,
I am very new to ThingWorx, and I am currently evaluating the product. I am trying to create a service in ThingWorx that will execute upon certain alert on a thing and post alert content to an external system via external system's REST API. With that, does ThingWorx offer a client lib like XMLHttpRequest for javascripting, or how do one normally handles authentication and GET/POST requests to external web APIs in ThingWorx?
Thanks,
Yuri
Hi Yuriy,
You should look at Snippets / ContentLoaderFunctions there you will find GET ( GetJSON, GetText,.. ), PUT ( PutJSON, PutText,... ) , POST and DELETE Calls.
Best Regards,
Thanks Carles! This worked out well. However, one more question arises: it seems like PostJSON snippet does not honor optional headers (JSON) parameter. At least I am not seeing any headers coming to my web server, when I specify them in the ThingWorx snippet and then try to trace them on my web server side. I wonder if I am doing something wrong, or maybe it's a bug. Thanks again.
I don't know sorry, you will need to dig by yourself.
Yuriy, are these parameters set to null or empty?
Thank you for your responses! So, I am setting headers parameter to one of my variables, named headers, which contains JSON-encoded value of headers string. But it has no effect, headers come empty to the web server.
var params = {
proxyScheme: undefined /* STRING */,
headers: headers /* JSON */,
ignoreSSLErrors: true /* BOOLEAN */,
useNTLM: false /* BOOLEAN */,
workstation: undefined /* STRING */,
useProxy: false /* BOOLEAN */,
withCookies: true /* BOOLEAN */,
proxyHost: undefined /* STRING */,
url: me.baseURL+me.srURL /* STRING */,
content: sr_dto /* JSON */,
timeout: 60 /* NUMBER */,
proxyPort: undefined /* INTEGER */,
password: undefined /* STRING */,
domain: undefined /* STRING */,
username: undefined /* STRING */
};
// result: JSON
var result = Resources["ContentLoaderFunctions"].PostJSON(params);
Just so i understand correctly which of these params come up empty? How are you setting it? While debugging it in the browser have you noted any errors?
Please be aware that ThingWorx REST API does not accept null parameter values (see below data types to which this is applied) and return Unable to To Parse JSON Request error 406 Not Acceptable
BLOB, BOOLEAN, DATETIME, IMAGE, INFOTABLE, INTEGER, LOCATION, LONG, JSON, NUMBER, QUERY, TAGS
Hi Sushant,
Thank you for response. The task that I am doing is very simple. I am creating a service, which uses PostJSON snippet. Above, I have posted the parameters that I am using. I apologize if I was not clear enough: I am posting from ThingWorx to a 3rd-party system (not to ThingWorx). Also, I am not using any null values, but leaving the as undefined, if I do not have any value for them. My main problem is that no matter what I put as headers /*JSON*/ (JSON-enconded string) input parameter for PostJSON function, HTTP request headers come empty to my other system from ThingWorx.
Thanks again,
Yuri
Thanks Yuriy for sharing the details. Have you checked the debugger for any possible hints there?
Thank you all! After trying different things what worked for me was grabbing the cookie that I had received during authentication and setting it in my subsequent requests in the headers as "Cookie" and not "Set-Cookie" (like browsers do), and then converting it to JSON payload, as below:
var json = me.Authenticate();
var cookie = {"Content-Type":"application/json","Cookie": json["_cookies"]};
var headers = JSON.stringify(cookie);
........ etc.......
I also believe this link might have some answers https://support.ptc.com/apps/solution_preview/solution/lang/presolution?lang=en&n=CS245905 but, unfortunately, I cannot see it at the moment, as I don't have support account. Still evaluating Thigworx...
Hi Yuriy,
Extending on Carles's response you can also check the ThingWorx REST API documentation if you haven't already examples on Updating, Deleting and Executing and also for the best practices needed when you are considering the REST API
Sushant
Yuriy, here's what the resolution contains for that article, (BTW could you try creating an account on support.ptc.com under ThingWorx if you haven't already)
Example Code:
// Define the headers that need to be sent with our service call // The "Cookie" header passes in a semi-colon separated list of cookies (attribute=value pairs) var headers = {"Cookie": "JSESSIONID=AB12397DCFB2342;myCookie=value123"}; var params = { headers: headers /* JSON */, url: "http://myserver.com/Services/MyService" /* STRING */, }; var result = Resources["ContentLoaderFunctions"].PostJSON(params);
Similarly, other headers can be added to the JSON request by adding additional attributes in the JSON object:
// Define all headers to be included with the request var headers = { "Cookie": "JSESSIONID=AB12397DCFB2342;myCookie=value123", "Content-Type": "application/json", "appKey": "a4823bdc48555aff" }; var params = { headers: headers /* JSON */, url: "http://myserver.com/Services/MyService" /* STRING */ }; var result = Resources["ContentLoaderFunctions"].PostJSON(params);
Thank you Sushant for all the help!