0

I have a curl POST request to do in java and i want to convert it to a simple HTTP request with a json file as body content, or to execute it without the conversion.

I tried to convert the request as a simple HTTP POST but it didn't work.

FileWriter file2 = new FileWriter("/home/oairan/NetBeansProjects/test1/src/post_slice.json");    
file2.write(json.toString());
file2.close();
URL obj = new URL("http://127.0.0.1:9999/slice/enb/-1 --data-binary @/home/oairan/NetBeansProjects/test1/src/post_slice.json");
HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json"); con.setRequestProperty("Method", "POST");
if(con.getResponseCode() == HttpURLConnection.HTTP_OK)
   {
    //some code here
}

The original curl request is:

curl -X POST http://127.0.0.1:9999/ue_slice_assoc/enb/-1 --data-binary "@file2"

can i execute it without using the HTTP request? Thank you in advance.

Nath94
  • 1
  • 1
  • 1
    The url in the code has `slice` whereas the one in the curl request has `ue_slice_assoc`. Is there a typo? – Madhu Bhat Jul 02 '19 at 13:27
  • 2
    This `"http://127.0.0.1:9999/slice/enb/-1 --data-binary @/home/oairan/NetBeansProjects/test1/src/post_slice.json"` is not an URL. –  Jul 02 '19 at 13:35
  • yes it's slice (sorry for the mistake) @MadhuBhat – Nath94 Jul 02 '19 at 13:41
  • @LutzHorn yes i know i couldn't convert the curl request properly. how can i solve it? – Nath94 Jul 02 '19 at 13:43
  • Check [parsing json from apache httpclient](https://stackoverflow.com/questions/23743370/parsing-json-from-httpclient-request-using-json-org-parser) – Victor Gubin Jul 03 '19 at 10:45

0 Answers0