title: How to perform a post request using json file as body
body: <p>I'm new in using REST call .
  I have a <code>test.json</code> file in my project.</p>
  
  <p>Content of the file is:</p>
  
  <pre><code>{
    "Added": {
      "type": "K",
      "newmem": {
        "IDNew": {
          "id": "777709",
          "type": "LOP"
        },
        "birthDate": "2000-12-09"
      },
      "code": "",
      "newest": {
        "curlNew": "",
        "addedForNew": ""
      }
    }
  }
  </code></pre>
  
  <p>Code in Java :</p>
  
  <pre><code>import java.io.DataInputStream;
  import java.io.File;
  //import org.json.JSONObject;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  import java.net.URL;
  import javax.net.ssl.HttpsURLConnection;
  import org.json.simple.JSONObject;
  import org.json.simple.parser.JSONParser;
  import org.json.simple.parser.ParseException;
  public class TestAuth {
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
  
          File file = new File("test.json");
             try {
                  JSONParser parser = new JSONParser();
                  //Use JSONObject for simple JSON and JSONArray for array of JSON.
                  JSONObject data = (JSONObject) parser.parse(
                        new FileReader(file.getAbsolutePath()));//path to the JSON file.
               System.out.println(data.toJSONString());
                  URL url2 = new URL("myURL");
                  HttpsURLConnection conn = (HttpsURLConnection) url2.openConnection();
                  conn.setRequestMethod("POST");
                  conn.setRequestProperty("Content-Type", "application/json");
                  conn.setRequestProperty("Accept", "application/json");
                  conn.setRequestProperty("Authorization", "Bearer aanjd-usnss092-mnshss-928nss");
  
                  conn.setDoOutput(true);
                  OutputStream outStream = conn.getOutputStream();
                  OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, "UTF-8");
                  outStreamWriter.write(data.toJSONString());
                  outStreamWriter.flush();
                  outStreamWriter.close();
                  outStream.close();
                  String response = null;
                  DataInputStream input = null;
                  input = new DataInputStream (conn.getInputStream());
                  while (null != ((response = input.readLine()))) {
                      System.out.println(response);
                      input.close ();
                  }
              } catch (IOException | ParseException e) {
                  e.printStackTrace();
              }
      }
  }
  </code></pre>
  
  <p>Exception:
  java.io.IOException: Server returned HTTP response code: 401 for URL: <a href="https://url_example.com/" rel="nofollow noreferrer">https://url_example.com/</a>
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
      at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:263)
      at ab.pkg.TestAuth.main(TestAuth.java:44)        </p>
  
  <p>In Soap Ui , Adding the Endpoint and above content as a request body for the POST request is a successful response .</p>
  
  <p>How can I read the json content and pass it as a request body in java ?</p>