POST / PUT should be used when modifying or creating a new instance on the internal server. When using a REST call in the form at http: ///// {parameter1} / {parameter2} (etc.) A request or sending a body is not sent! it should still be a POST call if it changes the data.
So, in this case, we can do some reflection.
String urlParameters = url.getQuery(); if (urlParameters == null) urlParameters = ""; byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); int postDataLength = postData.length; if (postDataLength > 0) { //in case that the content is not empty conn.setRequestProperty("Content-Length", Integer.toString(postDataLength)); } else { // Reflaction the HttpURLConnectioninstance Class<?> conRef = conn.getClass(); // Fetch the [requests] field, Type of MessageHeader Field requestsField= conRef .getDeclaredField("requests"); // The [requests] field is private, so we need to allow accessibility requestsField.setAccessible(true); MessageHeader messageHeader = (MessageHeader) requestsField.get(conn); // Place the "Content-Length" header with "0" value messageHeader.add("Content-Length", "0"); // Inject the modified headers requestsField.set(conn, messageHeader); }
Thus, we will not harm the existing model, and the call will be sent with a ZERO-length header.
Amour shmuel
source share