title: Java/Flask POST request
body: <p>I'm trying to send some data from my android application to my server running some Flask code. I'm using <code>HTTPUrlConnection</code> to make a <code>POST</code> request to the Flask server. I've been able to create a REST API using Flask but I'm not sure why my Java to Python communication isn't working. I get the following error <code>"POST /signUp HTTP/1.1" 400 -</code> from the server which I couldn't get much out of (debugging is turned on in Flask). How should I be passing JSON data to my server and how should my Flask code interpret this? </p>
  
  <p>My Java code is:</p>
  
  <pre><code>        URL url = new URL(url);
          connection = (HttpURLConnection) url.openConnection();
          connection.setRequestMethod("POST");
          connection.setRequestProperty("Content-Type", "application/json");
          connection.setDoInput(true);
          connection.setDoOutput(true);
  
          OutputStream stream = connection.getOutputStream();
          writer = new DataOutputStream(stream);
  
          Log.d("WARN", getQuery(params)); 
          // The LogCat prints out data like: 
          // ID:test,Email:test@gmail.com,Pwd:test
  
          writer.writeBytes(getQuery(params));
          writer.flush();
          writer.close();
          stream.close();
  
  // getQuery function aims to create a JSON string to send to the Flask server.
  private String getQuery(List&lt;Pair&lt;String, String&gt;&gt; params)
  {
      StringBuilder result = new StringBuilder();
      boolean first = true;
  
      for (Pair&lt;String, String&gt; pair : params)
      {
          if (first)
              first = false;
          else
              result.append(",");
  
          result.append(pair.first);
          result.append(":");
          result.append(pair.second);
      }
  
      return result.toString();
  }
  </code></pre>
  
  <p>My server-side code is:</p>
  
  <pre><code>@app.route("/signUp", methods=['POST'])
  def signUp():
    req_json = request.get_json()
    g.cursor.execute("INSERT INTO test (ID, Email, Pwd) VALUES (%s,%s,%s)", (req_json['ID'], req_json['Email'], req_json['Pwd']))
    g.conn.commit()
    resp = Response("Updated", status=201, mimetype='application/json')
    return resp
  </code></pre>