0

I have a client program in which i make a HTTPURLConnection to a RESTful web service. I am passing all JSON data in the http request object. Everything seems to work fine for the HTTP POST/GET method but not for the DELETE method. When i execute the following code to delete a record, I am running into this exception...

java.net.ProtocolException: HTTP method DELETE doesn't support output
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1081) 

My java code is this

URL url = new URL("http://example.com/api/indexer/v1/delete");   
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("DELETE");
conn.setRequestProperty("Content-Type", "application/json");

String input = "{\"ref\" : 123456}";

    OutputStream os = conn.getOutputStream();
    os.write(input.getBytes());
    os.flush();

    System.out.println(conn.getResponseCode() );

So How to perform operation delete. WEbservice requests parameter input.

manitaz
  • 1,181
  • 2
  • 9
  • 26
  • Given the error message, `conn.setDoOutput(true);` would seem to be incompatible with `DELETE`. – Ken Y-N Nov 18 '14 at 06:24
  • java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true) so how to set body to delete method? is it not allowed? – manitaz Nov 18 '14 at 06:25
  • when I check the service in advanced rest client it is working – manitaz Nov 18 '14 at 06:27
  • The [first hit for your error message](http://stackoverflow.com/a/20075713/1270789) suggests (a) there is a bug in Java 7 and (b) a workaround. – Ken Y-N Nov 18 '14 at 06:31
  • Please read the linked answer - there is a workaround described in detail there. – Ken Y-N Nov 18 '14 at 06:44

0 Answers0