title: How to POST to node.js from applet?
body: <p>I have an applet (not our choice, it's the MarioAI engine) that I'd like to connect to a node.js app that uses express...but I can't seem to get mongodb to accept the values I'm sending in my POST request through localhost. I keep getting 200 response from node, but 'undefined' from mongooose, which I suspect means the URLEncoder I'm using in Java is mangling the String I'm sending through somehow.</p>
  
  <p>I read this:</p>
  
  <p><a href="https://stackoverflow.com/questions/4989120/problem-with-java-applet-to-connect-our-server-to-call-a-php-file">Problem with Java Applet to connect our server to call a PHP file</a></p>
  
  <p>and tried the following OutputStreamWriter call in Java:</p>
  
  <pre class="lang-java prettyprint-override"><code>//EvaluateFrustration() takes an int but should come back with a float value
  String frustrationString = Double.toString(EvaluateFrustration(this.periods));
  try {
      URL url = new URL("http://127.0.0.1:8888/mario");               
      final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setDoOutput(true);
      conn.setRequestMethod("POST");
      System.out.println(conn.getResponseCode());
      conn.setUseCaches (false);
      conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  
      OutputStreamWriter writer;
  
  
      writer = AccessController
                  .doPrivileged(new PrivilegedExceptionAction&lt;OutputStreamWriter&gt;() {
                      public OutputStreamWriter run() throws IOException {
                          return new OutputStreamWriter(conn.getOutputStream());
                      }
                  });
          String data = URLEncoder.encode("frustrationValueFirstRound=" 
                  + frustrationString,"UTF-8");
          writer.write(data);
          writer.flush();
  
  } catch (Exception e) {
  }
  </code></pre>
  
  <p>In the node app (using express and mongoose/mongodb), I wrote:</p>
  
  <pre class="lang-js prettyprint-override"><code>var express = require('express');
  
  var mongoose = require('mongoose');
  
  var Schema = mongoose.Schema
    , ObjectId = Schema.ObjectId;
  
  var ExperimentSchema = new Schema({
      experiment    : ObjectId
      , frustrationValueFirstRound : Number
  });
  
  mongoose.connect('mongodb://localhost/mariopaper');
  mongoose.model('Experiment', ExperimentSchema);
  var Experiment = mongoose.model('Experiment');
  
  app.post('/mario', function(req, res){
    var exp = new Experiment();
    exp.frustrationValueFirstRound = req.body.frustrationValueFirstRound;
    exp.save(function(err){   if (err) { throw err; }
    res.send('ok');
  });
  </code></pre>
  
  <p>For reference, I'd like to point out that this curl call works just fine:</p>
  
  <pre><code>curl -d "frustrationValueFirstRound=99" http://localhost:8888/mario
  </code></pre>
  
  <p>Anyone have any ideas whether I've simply written the POST wrong in Java, or perhaps I'm missing something in how URLEncoder.encode() works?</p>