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
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);
}
Solved! Go to Solution.
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?
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?
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);
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