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

Community Tip - You can change your system assigned username to something more personal in your community settings. X

PostJSON and PutJSON in ThingWorx 9.2

RK_0705
12-Amethyst

PostJSON and PutJSON in ThingWorx 9.2

Hi,

 

How to use these two services of contentloaderfunctions.

 

We need to pass body as json for getting the response.

 

Please share the examples of both services especially PUT.

 

Thanks in advance.

3 REPLIES 3

I don't have an example of PUT but it should be very similar to the Post Example.  For context the example below is making a post request to get an application TOKEN back and setting some thing properties.

try
{
	let content = {
		"Username": me.username,
		"Password": me.password
	};
	var response = Resources["ContentLoaderFunctions"].PostJSON({
		ignoreSSLErrors: true,
		url: me.baseURL + me.tokenEndpoint /* STRING */ ,
		content: content,
		contentType: "application/json"
	});
	
	var now = new Date();
	if (response.body.token && response.body.token != "") {
		me.token = response.body.token;
		me.lastSuccessfulAPICall = now;
		me.tokenExpirationDate = dateAddHours(now, me.tokenLifetimeHours);
		result = true;
	}
}
catch (err) {
	logger.error(me.name + ", error in GetToken service: " + err);
}

 

Thanks,

 

Travis

VladimirN
24-Ruby II
(To:RK_0705)

Here are some helpful materials:

 

"REST API Overview and Examples": https://community.ptc.com/t5/IoT-Tech-Tips/REST-API-Overview-and-Examples/td-p/534777

 

"How to call the RESTful API in a ThingWorx platform service": https://www.ptc.com/en/support/article/CS237875

 

"ThingWorx: Difference between GET, POST, and PUT REST methods": https://www.ptc.com/en/support/article/CS214689 

 

"How to Post XML or JSON Content to a Custom Thingworx Service via REST API": https://www.ptc.com/en/support/article/CS201979

 

"ThingWorx: Post JSON to a service that accepts JSON as an input parameter": https://www.ptc.com/en/support/article/CS213601

 

Hi @RK_0705 ,

 

Try something like this:

 

var headers = {
	"header1": me.header1,
	"header1": me.header2
};

var body= {
	"param1": me.param1,
	"param2": me.param2
};

var directURL = "<Your_Endpoint_URL>";

// It returns JSON
var result = Resources["ContentLoaderFunctions"].PostJSON({
	url: directURL /* STRING */,
	content: body /* JSON */,
	headers: headers /* JSON */,
	useProxy: true /* BOOLEAN */,
});

 

Note: I created header and body parameters as a Thing properties, but you can pass them as a service parameters too. Hope this helps.

 

Thanks,

Shirish 

Announcements


Top Tags