2 Replies Latest reply on May 25, 2017 1:31 PM by rupadhyay-2 RSS
    pagrawal-2 Apprentice

    Hey,I am just doing simply the TWX Rest Api Call from a very simple java code.

    What I want is that just simply pass a variable from Java code to TWX service and just want to check that is that value is coming in TWX or not,then I will have some other work with that variable.

    I just wrote a simple code and establish the connection with my Local TWX and tried to pass that String value named "id".

    And then on TWX side, I just created a thing and created a service and take an input parameter named "id" and simply did "logger.warn(id);" in script editor.

    And after running that Java code on Eclipse, I am getting 400 Error (i.e. some forbidden request error) on Eclipse and on TWX, in the Logs I am getting

    "[context: com.thingworx.webservices.context.HttpExecutionContext@66efd9][message: Invalid Invocation Request]" this error.

     

    My Java Code is :

     

    package Call;

    import java.io.OutputStream;

    import java.net.HttpURLConnection;

    import java.net.URL;

    import java.nio.charset.Charset;

     

    public class TWRestCallSample

    {

      public static void main(String[] args)

          {

                 try    {      callTW("RowId_2");      // dummy id value       }

                 catch (Exception e)      {       e.printStackTrace();       }

        }

     

      static void callTW(String id) throws Exception

         {

                String statusMsg = "";

               try {

                               String uri ="http://localhost:8080/Thingworx/Things/test/ServiceDefinitions/test1?appKey=d77e8e3a-209f-44ea-8a2f-6ee88c6cf0c7";

                               System.out.println(uri);

                               HttpURLConnection conn = (HttpURLConnection) new URL(uri).openConnection();

            

                                 conn.setDoOutput(true);

                                 conn.setRequestMethod("POST");

                                 conn.setRequestProperty("Content-Type","application/json;charset=UTF-8");

            

                                 OutputStream os = conn.getOutputStream();

     

                                 os.write(id.getBytes(Charset.forName("UTF-8")));

                                 os.flush();

                         

             System.out.println("1");

        

                                 if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {

                                          statusMsg = conn.getResponseCode() + " " + conn.getResponseMessage();

                                                 throw new RuntimeException("Failed : HTTP error code : " + statusMsg);

                                                     }

     

             System.out.println("2");

             os.close();       

             conn.disconnect();

            

      }

     

    catch (Exception e){

      throw new Exception(statusMsg + "." + e);

          }

      }

    }

     

     

    And on TWX Side, I just created Thing->test and in that the service->test1 and in the service Input/Output Parameter is "id" of String type.

     

    If you get any idea why is this happening then please tell me.

    I got stuck there too hard.

    Thanks.