title: HTTP POST request in JAVA with payload as a json file
body: <p>Below is what i tried to send a HTTP POST request which send the json file as payload. The Error I always get is </p>
  
  <p><strong>java.io.FileNotFoundException: test.json (The system cannot find the file specified)</strong></p>
  
  <p>Although the test.json file is in the same folder.</p>
  
  <pre><code>private void sendPost() throws Exception {`
  
      String url = "url";
      HttpClient httpClient = HttpClientBuilder.create().build();
      HttpPost post = new HttpPost(url);
  
      List&lt;NameValuePair&gt; urlParameters = new ArrayList&lt;NameValuePair&gt;();
  
      String postData = AutomaticOnboarding.readFile("test.json");
      urlParameters.add(new BasicNameValuePair("data", postData));
      StringEntity se = new StringEntity(postData);
      post.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
  
      post.setEntity(se);
  
      HttpResponse response = httpClient.execute(post);
      System.out.println("\nSending 'POST' request to URL : " + url);
      System.out.println("Post parameters : " + post.getEntity());
      int responseCode = response.getStatusLine().getStatusCode();
      System.out.println("Response Code : " +responseCode);
      if(responseCode == 200){
          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());
      }
  }
  </code></pre>
  
  <p>Here follows the readFile method:</p>
  
  <pre><code>public static String readFile(String filename) {
      String result = "";
      try {
          BufferedReader br = new BufferedReader(new FileReader(filename));
          StringBuilder sb = new StringBuilder();
          String line = br.readLine();
          while (line != null) {
              sb.append(line);
              line = br.readLine();
          }
          result = sb.toString();
      } catch(Exception e) {
          e.printStackTrace();
      }
      return result;
  }
  </code></pre>