0
votes

i have problems with my http-headers. I need to send a post-request with an empty body and the header "Content-Length:0". But this seems impossible, becauce i get this exception:

--org.apache.http.ProtocolException: Content-Length header already present--

But when i do not include the header, there is no Content-Length header in the request and therefore i get 400: Bad Request. I build this in Python too and there it works fine, but i can not make it work in java. Can you help me, please?

Here is my Code:

HttpClient client = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(header..);
httpPost.addHeader(another header);
httpPost.addHeader("Content-Type","application/xml");
httpPost.addHeader("Content-Length","0");
httpPost.addHeader(another header);
HttpResponse response = client.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());

EDIT:

Solved the problem with the answer from @Beno Arakelyan.

3
FWIW, sending a Content-Type if you not send a request body doesn't make any sense. - Julian Reschke
Your comment is not helpful @Reschke. We could discuss this post (wich performs an action wich does not need a body), but it is not my api. I just have to use this api and i can not change it. If i could, i would change more than just this :D - TheJed
Are you sure you need to send Content-Length? - Julian Reschke

3 Answers

1
votes

I tried to send the Content-Length with http-request which built on apache http client. It works fine. Example:

private static final HttpRequest<?> HTTP_REQUEST =
      HttpRequestBuilder.createGet(YOUR_URI)
        .addDefaultHeader(HttpHeaders.CONTENT_LENGTH, "0")
        .build();

public void test(){
  System.out.println(HTTP_REQUEST.execute().getStatisCode()); // is 200
}
0
votes

You don't need to calculate the content length, the client will handle that for you. Try removing the header and set a body instead, for example: httpPost.setEntity(new StringEntity("")).

0
votes

This Link directs you to the example below

URL url = new URL("https://reqbin.com/echo/post/json");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setRequestMethod("POST");
http.setDoOutput(true);
http.setRequestProperty("Content-Type", "application/json");

String data = "{\"widget\": {\n    \"window\": {\n        \"title\": \"Sample Widget\",\n        \"name\": \"sample_widget\",\n        \"width\": 600,\n        \"height\": 400\n    },\n    \"image\": { \n        \"src\": \"images/test.png\",\n        \"name\": \"sample_test\",\n        \"hOffset\": 150,\n        \"vOffset\": 150,\n        \"alignment\": \"center\"\n    },\n    \"text\": {\n        \"data\": \"Click Here\",\n        \"size\": 36,\n        \"style\": \"bold\",\n        \"name\": \"sample_click\",\n        \"alignment\": \"center\"\n    }\n}} ";

byte[] out = data.getBytes(StandardCharsets.UTF_8);

OutputStream stream = http.getOutputStream();
stream.write(out);

System.out.println(http.getResponseCode() + " " + http.getResponseMessage());
http.disconnect();

Note: Content-Length will be added automatically.