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

Service to authenticate and POST to external system

Newbie

Service to authenticate and POST to external system

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

Tags (2)
12 REPLIES 12
Highlighted

Re: Service to authenticate and POST to external system

Hi Yuriy,

You should look at Snippets / ContentLoaderFunctions there you will find GET ( GetJSON, GetText,.. ), PUT ( PutJSON, PutText,... ) , POST and DELETE Calls.

Best Regards,

Highlighted

Re: Service to authenticate and POST to external system

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.

Highlighted

Re: Service to authenticate and POST to external system

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

Highlighted

Re: Service to authenticate and POST to external system

I don't know sorry, you will need to dig by yourself.

Highlighted

Re: Service to authenticate and POST to external system

Yuriy, are these parameters set to null or empty?

Highlighted

Re: Service to authenticate and POST to external system

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);

Highlighted

Re: Service to authenticate and POST to external system

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

Highlighted

Re: Service to authenticate and POST to external system

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

Highlighted

Re: Service to authenticate and POST to external system

Thanks Yuriy for sharing the details. Have you checked the debugger for any possible hints there?

Announcements

Thingworx Navigate content has a new home! Click here to access the new Thingworx Navigate forum! ______________________________