2 Replies Latest reply on Apr 11, 2016 3:46 AM by kboucheron RSS
    kboucheron Newbie

    Using REST API with java to invoke service with parameters

    Hi,

     

    I currently try to use a POST request to send json data to a service on the TWX platform.

     

    I successed to invoke the service, i can see it in the script log.

    The thing invoked take 2 optionnal inputs :

    • value a TEXT, just to test the send...
    • jsonObj is the JSON object i want to send, to process it after

     

    But here is the problem : both of these inputs stay at undefined state, and i didn't manage to get the data....

     

    Here is the Java code used to send the request :

     

    HttpsURLConnection conn =

      (HttpsURLConnection) url.openConnection();

    byte[] postData = jsonData.getBytes(StandardCharsets.UTF_8);
    int postDataLength = postData.length;
    conn.setRequestMethod("POST");
    conn.setRequestProperty("x-thingworx-session", "true");
    conn.setRequestProperty("Accept", "application/json");
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
    conn.setRequestProperty("appKey", appKey);
    // Create the form content
    conn.setDoOutput(true);
    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
    out.write(postData);
    out.flush();
    out.close();
    if (conn.getResponseCode() != 200) {

       throw new IOException(conn.getResponseMessage());
    }


    Can comeone help me to understand why my jsonObj is still undefined ? The JsonData is a String got from a JSONObject.


    Thanks,

    Kevin B.