12 Replies Latest reply on Aug 5, 2015 12:54 AM by shashikanta RSS
    shashikanta Explorer

    Why is CreateThing java REST call in R6 is failing with HTTP Error 403 Forbidden?

    The method=post is not allowed in URL parameter in TWX server R6 version due to security issues. So modified the code to use post like below. However the request still fails. I have ensured that URL as well as appKey is correct. Any Ideas ?

     

    URL thingworx_rest_url = new URL(

      ACConfig.REST_BASEURI + "Resources/EntityServices/Services/CreateThing");

     

      HttpURLConnection http_connection = (HttpURLConnection) thingworx_rest_url

      .openConnection();

      http_connection.setDoOutput(true);

      http_connection.setRequestMethod("POST");

      http_connection.setRequestProperty("appKey", ACConfig.AUTH_APPKEY);

      http_connection.setUseCaches(false);

     

      String input = "name="

      + ACConfig.ACThingName + "&description="

      + ACConfig.ACThingName

      + "&thingTemplateName=RemoteThing";

     

      byte[] postData       = input.getBytes( StandardCharsets.UTF_8 );

      int postDataLength = postData.length;

     

      http_connection.setInstanceFollowRedirects( false );

      http_connection.setRequestMethod( "POST" );

      http_connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");

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

      }

     

     

      if (http_connection.getResponseCode() != HttpURLConnection.HTTP_CREATED) {

      throw new RuntimeException("Failed : HTTP error code : "

      + http_connection.getResponseCode());

      }