0

I use command "curl -X DELETE --header 'Accept: application/json' 'http://10.10.1.29:8181/onos/v1/flows/application/olsrflow' -u karaf:karaf"

It Can work.

but use JAVA Code does't work.

have any problem in my JAVA code?

     URL dc0ContrailUrl2 = new URL("http://10.10.1.29:8181/onos/v1/flows/application/olsrflow");
     HttpURLConnection dcConn2 = (HttpURLConnection) dc0ContrailUrl2.openConnection();
     dcConn2.setDoOutput(true);
     String login = "karaf:karaf";
     String content = URLEncoder.encode (login) ;
     String basicAuth = "Basic " + new String(new Base64().encode(login.getBytes()));
     dcConn2.setRequestProperty("Authorization",basicAuth);
     dcConn2.setRequestProperty("Content-Type", "application/json");
     dcConn2.setRequestMethod("DELETE");
     BufferedReader in2 = new BufferedReader(new InputStreamReader(dcConn2.getInputStream()));

     String inputLine2;

 while ((inputLine2 = in2.readLine()) != null){ //while response is not null, assign response to inputLine and print inputLine
         System.out.println(inputLine2); 
 }
         in2.close();

error HTTP response code: 415

葉又銘
  • 1
  • 1
  • There is a difference in header (curl command) and request properties (java code). For more info see: https://webmasters.stackexchange.com/questions/31212/difference-between-the-accept-and-content-type-http-headers – BitfulByte Apr 25 '18 at 12:24

2 Answers2

1

You did not add Accept type the same as curl. Add the following line and remove the Content-Type:

 dcConn2.setRequestProperty("Accept", "application/json");
Mostafa Barmshory
  • 1,849
  • 24
  • 39
  • 1
    Wouldn't you get a 406 Not Acceptable if Accept was the problem? 415 often is about the Content-Type. In the cURL request the OP is not setting the Content-Type, but in the Java code they are. Maybe just removing the Content-Type in the Java code would solve the problem. – Paul Samsotha Apr 26 '18 at 20:02
  • Dear Paul, you are right. Actually I mean replace content with accept. – Mostafa Barmshory Apr 27 '18 at 11:13
-1

Typically it means that server does not support MediaType has been passed. You have to figure out what does it expect and setup it in your request accordingly.

AlexGera
  • 756
  • 9
  • 19