Skip to main content
1-Visitor
January 25, 2021
Solved

Connect to ThingWorx to get data

  • January 25, 2021
  • 2 replies
  • 2033 views

Hi All,

 

I am attempting to connect to thingworx using Java to get specific data using a key.

Data Table Name: DataTable

Service I want to use to extract the data for a row : GetDataTableEntry

This is a generic service and I need to pass key to get the data which is working fine when I use SOAPUI to connect.

In below java code, the row gets returned as null. 

 

ClientConfigurator config = new ClientConfigurator();

config.setUri("wss://URL:443/Thingworx/WS");

config.setAppKey("key");

config.ignoreSSLErrors(true); // All self signed certs

ConnectedThingClient client = new ConnectedThingClient(config);

client.connect();
client.start();


ValueCollection params = new ValueCollection();
params.put("key", new StringPrimitive("Test20124"));

InfoTable table = client.invokeService(ThingworxEntityTypes.Things, "DataTable", "GetDataTableEntry", null, 5000);
System.out.println(table.getJSON());

 

This Returns null. 

 

Similarly even if I directly attempt to call rest service, the data returned is null

 

HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("key", "Test20124");
con.setRequestProperty("Accept", "application/json");
con.setInstanceFollowRedirects( false );
con.setUseCaches( false );
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("appKey", "key");


con.setDoOutput(true);

try( DataOutputStream wr = new DataOutputStream( con.getOutputStream())) {
wr.flush();
wr.close();
}

int responseCode = con.getResponseCode();

System.out.println("\nSending 'POST' request to URL : " + sURL);

System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

String inputLine;

StringBuilder response = new StringBuilder();

while((inputLine = in.readLine()) != null) {

response.append(inputLine);

}

Best answer by PaiChung

I'm wondering about your URL, almost as if you are connecting 'posing' as an agent.

Usually for TWX Rest API calls the URL would be

<server>:port/Thingworx/Things/NameOfThing/Services/NameOfService

and then in the headers you would provide your appkey and in the body your parameters

Have you tried using Postman or something similar first?

2 replies

PaiChung22-Sapphire IAnswer
22-Sapphire I
January 26, 2021

I'm wondering about your URL, almost as if you are connecting 'posing' as an agent.

Usually for TWX Rest API calls the URL would be

<server>:port/Thingworx/Things/NameOfThing/Services/NameOfService

and then in the headers you would provide your appkey and in the body your parameters

Have you tried using Postman or something similar first?

1-Visitor
January 28, 2021

Adding to what Pai commented:

-in the first try, you don't pass the parameters to your service - they are null

InfoTable table = client.invokeService(ThingworxEntityTypes.Things, "DataTable", "GetDataTableEntry", null, 5000);

Community Manager
February 11, 2021

Hi @RaghuNarayanan.

 

If you feel your question has been answered, please mark the appropriate response as the Accepted Solution for the benefit of others with the same question.

 

Regards.

 

--Sharon