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

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

REST API handling post request

rohitk
1-Visitor

REST API handling post request

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

7 REPLIES 7
paic
12-Amethyst
(To:rohitk)

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.

One of the Input parameters into the service will be either String or JSON so your call can pass that into ThingWorx.

rohitk
1-Visitor
(To:paic)

yes correct but it has to be "post" request not "get".



rohitk
1-Visitor
(To:paic)

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.




paic
12-Amethyst
(To:rohitk)

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.




paic
12-Amethyst
(To:rohitk)

In addition please look at our Expert blog on the REST API, that might help as well.

paic
12-Amethyst
(To:rohitk)

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)



vranganathan
12-Amethyst
(To:paic)

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

    }

Announcements


Top Tags