How to handle JSON in Meteor (Sending POST Req from a JAVA program)


#1

SOLVED! Thanks

Should’ve used
a.body.name

Hello,
How do I properly handle a POST with a JSON file on Meteor?

Using Iron-router package.

I have this in my server main.js

Meteor.startup(() => {

 Router.route( "sendStats", { where: "server" } )
  .get( function() {
    // If a GET request is made, return the user's profile.
	console.log("get");
  })
  .post( function(a, b) {
    // If a POST request is made, create the user's profile.
	console.log("POST REQUEST");
	var s = JSON.parse(a).name;
	console.log(s);
	
  })
  .put( function() {
    // If a PUT request is made, either update the user's profile or
   // create it if it doesn't already exist.
   console.log("put");
  })
  .delete( function() {
   // If a DELETE request is made, delete the user's profile.
   console.log("delete");
  });
  
	


});

This is the code I have in java application

 private void sendingPostRequest() throws Exception {

        String url = "http://127.0.0.1:3000/sendStats";

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // Setting basic post request
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", USER_AGENT);
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        con.setRequestProperty("Content-Type","application/json");

       String data[] = new String[2];
data[0] = "Kevin"; data[1] = "21";


        String str = "{ \"name\": \""+data[0]+"\", \"age\": "+data[1]+" }";


        String postJsonData = str;

  


        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
       // for(String s : data)
        wr.writeBytes(postJsonData);

        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        log("nSending 'POST' request to URL : " + url);
        log("Post Data : " + postJsonData);
        log("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String output;
        StringBuffer response = new StringBuffer();

        while ((output = in.readLine()) != null) {
            response.append(output);
        }
        in.close();

        //printing result from response
        System.out.println(response.toString());
    }

I get errors when I try to use

	var s = JSON.parse(a).name;

If I just try to print out a or b, it just says Object object.

Error I am getting

`I20180123-20:09:12.671(2)? NEW POST
W20180123-20:09:12.674(2)? (STDERR) SyntaxError: Unexpected token u in JSON at position 0
W20180123-20:09:12.675(2)? (STDERR)     at JSON.parse (<anonymous>)
W20180123-20:09:12.676(2)? (STDERR)     at ctor.<anonymous> (server/main.js:45:15)
W20180123-20:09:12.677(2)? (STDERR)     at boundNext (packages\iron_middleware-stack.js:408:31)
W20180123-20:09:12.678(2)? (STDERR)     at runWithEnvironment (packages\meteor.js:1240:24)
W20180123-20:09:12.679(2)? (STDERR)     at packages\meteor.js:1253:14
W20180123-20:09:12.680(2)? (STDERR)     at boundNext (packages\iron_middleware-stack.js:355:14)
W20180123-20:09:12.681(2)? (STDERR)     at runWithEnvironment (packages\meteor.js:1240:24)
W20180123-20:09:12.682(2)? (STDERR)     at packages\meteor.js:1253:14
W20180123-20:09:12.684(2)? (STDERR)     at ctor.urlencodedParser (C:\Users\Aleksei\AppData\Local\.meteor\packages\iron_router\1.1.2\npm\node_modules1\body-parser\lib\types\urlencoded.js:77:44)
W20180123-20:09:12.685(2)? (STDERR)     at packages\iron_router.js:886:36`