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
I have to expose one REST API which will be handling post request in thingsworx.
Requirement :
1. API which will handle post request and extract request post data in form of json.
2. API will be called from external system
Hi Rohit, if I understand you correctly then all you need to do is to create a Service in ThingWorx that the external system can call.
yes correct but it has to be "post" request not "get".
The data of the device is in the form of JSON and i need to provide the service to the device gateway so that they can send the device data over HTTP post.
Hi Rohit, is the ThingWorx server contacting the Gateway for the information or is the Gateway posting the information to Thingworx?
Generally any device/external source can post to ThingWorx as follows
ServerAddress/Thingworx/Things/NameofThing/Services/NameofService?method=post&NameOfParameter=ValueOfParameter ...
Additional to that you can add &appKey=YourappKey or pass in the User name and Password which we do not recommend.
Those Parameters can be of any baseType you define.
If the Gateway has the ThingWorx Edge MicroServer running on it, you can do it as follows:
Doing an Action header of Post to
ServerAddress:8000/Thingworx/Things/NameofThing/Services/NameofService
with the Values object being a JSON value pair of the name of your parameter and the value of it.
If the ThingWorx server is contacting the Gateway, there are build in Services under Snippets/Resources/Contentloader functions called Load/Post JSON or XML, TEXT, Binary.
If this is not clear, we can schedule a phone call at your convenience.
In addition please look at our Expert blog on the REST API, that might help as well.
I apologize looks like I put the uri for a GET not a POST.
For a POST you would do:
ServerAddress/Thingworx/Things/NameofThing/Services/NameofService?method=post&postParameter=<name of input parameter that gets the post document>&Accept=application/json
(use Accept=text/xml for xml content)
I am getting the following error.
Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 500
at com.thingworx.http.client.HttpConnectionClient.main(HttpConnectionClient.java:76)
Here is the snippet which does my HTTP Connection.
http_connection.setInstanceFollowRedirects( false );
http_connection.setRequestMethod( "POST" );
//http_connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
http_connection.setRequestProperty( "Content-Type", "text/xml");
http_connection.setRequestProperty( "charset", "utf-8");
http_connection.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
try( DataOutputStream wr = new DataOutputStream( http_connection.getOutputStream())) {
wr.write( postData );
}