0

Hello I am having a lot of issues passing data to my parse.com cloud code function. I am trying to call my cloud code using Parse.com REST services. Below is my code:

private static void pushWithParse() throws IOException
{
    String url = "https://api.parse.com/1/functions/testCloudCode";

    HttpURLConnection connection = null;  
      try 
      {
        //Create connection
        URL urlRequest = new URL(url);
        System.out.println(urlRequest.toString());

        connection = (HttpURLConnection)urlRequest.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("X-Parse-Application-Id", "appID");
        connection.setRequestProperty("X-Parse-REST-API-Key", "appKey");
        connection.setRequestProperty("Content-Type","application/json; charset=UTF-8");
        connection.setRequestProperty("Accept-Charset", "UTF-8");
        connection.setDoOutput(true);

        OutputStream out = connection.getOutputStream();
        Writer writer = new OutputStreamWriter(out, "UTF-8");

        writer.write("username");
        writer.write("=");
        writer.write(URLEncoder.encode("joey", "UTF-8"));

        InputStreamReader isr = new InputStreamReader(connection.getInputStream(), "UTF-8");
        BufferedReader rd = new BufferedReader(isr);
        StringBuilder response = new StringBuilder(); 
        String line;
        while((line = rd.readLine()) != null) {
          response.append(line);
          response.append('\r');
        }
        writer.close();
          out.close();
        rd.close();
        System.out.println(response.toString());

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

      }
      finally
      {
            if(connection != null)
            {
              connection.disconnect(); 
            }
      }

}

I am getting a response from the server with a success. I am trying to print out the username but all I get is this:

Input: {}
Result: Yay need to fix push NOTIFICATIONs
I2015-07-06T17:45:25.223Z]___________________________________________________
I2015-07-06T17:45:25.224Z]No Message provided
I2015-07-06T17:45:25.225Z]No Message provided

for my cloud code:

Parse.Cloud.define("testCloudCode", function(request, response) 
{
    var parametersPassed = request.params.username;
    var requestBody =  request.requestBody;
    console.log("___________________________________________________");
    console.log(requestBody);
    console.log(parametersPassed);
    console.log("___________________________________________________");
    response.success("Yay need to fix push NOTIFICATIONs");
});

I have tried following various posts about passing data in Java but with no luck still getting no message to display. Thank you in advance.

  • I have followed the other articles and the others did not work that is why I posted this question since the duplicates were not working. – JMStudios.jrichardson Jul 06 '15 at 21:10
  • I am able to send data through Advanced Chrome extension and send something in the body, which means to me I am not writing data properly to my request. Someone please help – JMStudios.jrichardson Jul 07 '15 at 22:22
  • I have found the answer. This whole time I was trying to pass only a string when parse.com only accepts json data. You need to pass your object as a string so if i wanted to pass a username: writer.write("username"); writer.write("="); writer.write(URLEncoder.encode("joey", "UTF-8")); you need to pass it as a son string out.write("{\"username\": \"joey\"}".getBytes("UTF-8")); – JMStudios.jrichardson Jul 08 '15 at 01:42

0 Answers0