1

I would like to send this JSON message below, with the HTTP DELETE method. For this project is necessary to use OAuth2. So I use the dependency google-oauth. Foreach HTTP request I use the dependency google client.

{
   "propertie" : true/false,
   "members" : [
      "String value is shown here"
   ]
}

In my project I used this code below, but I cannot send the JSON message with the HTTP DELETE method.

Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(accessToken);
JsonArray members = new JsonArray();
JsonPrimitive element = new JsonPrimitive("String value is shown here");
members.add(element);

JsonObject closeGroupchat = new JsonObject();
closeGroupchat.addProperty("propertie", false);
closeGroupchat.add("members", members);
Gson gson = new Gson();
HttpContent hc = new ByteArrayContent("application/json", gson.toJson(closeGroupchat).getBytes());

HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
HttpRequest httpreq = requestFactory.buildRequest(HttpMethods.DELETE, genericUrl, hc);
return httpreq.execute();

Next error occurs:

java.lang.IllegalArgumentException: DELETE with non-zero content length is not supported

Can someone help me with this problem?

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Michael
  • 101
  • 2
  • 8
  • There is not a lot of solutions here: either you stick with the DELETE request, but as your error message says, you can't have a body on that kind of request, or you do it with a POST request, which will accept a body in the request, in which you will be able to send your JSON data – DamCx Jan 19 '18 at 13:49

1 Answers1

0

Your problem is that your HTTP DELETE request contains a body, which it shouldn't. When you delete a resource, you need to supply the URL to delete. Any body in the HTTP request would be meaningless, because the intention is to instruct the server to delete a resource at a particular URL. The same thing can often happen for GET requests - a body is meaningless because you're trying to retrieve a body.

For details on HTTP DELETE request bodies, see this SO question on the subject. Note that while request bodies may technically be permitted by the HTTP spec, based on your error message, your client library doesn't allow it.

To fix the problem, try passing a null value for hc, or a value that just wraps an empty JSON string (by which I mean using "", not "{}").

entpnerd
  • 10,049
  • 8
  • 47
  • 68