title: Java Post request with form data
body: <p>I want to make a simple POST call in Java,<br>
  I am getting a 200 response code but, with the wrong response message,<br>
  I am told there is a different way to make a Post call when using a <strong>form data</strong>.<br><br>
  <strong>Following is my current Java code to make the post call -<br></strong></p>
  
  <pre><code>private String makePostCall(){
          try {
              String url = "http://someIp/trusted";
              HttpClient client = new DefaultHttpClient();
              HttpPost post = new HttpPost(url);
  
              // add header
              List&lt;NameValuePair&gt; urlParameters = new ArrayList&lt;NameValuePair&gt;();
              urlParameters.add(new BasicNameValuePair("username", "app_user"));
  
              post.setEntity(new UrlEncodedFormEntity(urlParameters));
  
              HttpResponse response = client.execute(post);
              System.out.println("\nSending 'POST' request to URL : " + url);
              System.out.println("Post parameters : " + post.getEntity());
              System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
  
              BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
              StringBuffer result = new StringBuffer();
              String line = "";
              while ((line = rd.readLine()) != null) {
                  result.append(line);
              }
  
              System.out.println(result.toString());
              return result.toString();
  
          } catch (Exception e) {
              e.printStackTrace();
              return null;
          }
      }
  </code></pre>
  
  <p><strong>Following is the Post call sample that is working through Postman app -</strong>
  <a href="https://i.stack.imgur.com/pghqy.png" rel="noreferrer"><img src="https://i.stack.imgur.com/pghqy.png" alt="enter image description here"></a></p>
  
  <p><strong>I am referring the following website -</strong><br>
  <a href="https://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/" rel="noreferrer">https://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/</a></p>
  
  <p><br>The expected outcome of the post call is supposed to be a Token ie. a String value, current response is -1.</p>