7 Replies Latest reply on Dec 21, 2015 4:37 PM by varathanran RSS
    rohitk Explorer

    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

      • REST API handling post request
        paic Collaborator

        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.

          • REST API handling post request
            rohitk Explorer

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

             



            • REST API handling post request
              rohitk Explorer

              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.


               



                • REST API handling post request
                  paic Collaborator

                  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.




                  • REST API handling post request
                    paic Collaborator

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

                     

                • REST API handling post request
                  paic Collaborator

                  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)



                    • Re: REST API handling post request
                      varathanran Apprentice

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

                          }