Uploaded image for project: 'HttpComponents HttpClient'
  1. HttpComponents HttpClient
  2. HTTPCLIENT-2245

Content-Length is missing for HTTP/2

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 5.1.3, 5.2-beta1
    • None
    • HttpClient (async)
    • None
    • macOS, JDK 17

      openjdk version "17" 2021-09-14
      OpenJDK Runtime Environment Temurin-17+35 (build 17+35)
      OpenJDK 64-Bit Server VM Temurin-17+35 (build 17+35, mixed mode, sharing)

    Description

      import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
      import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
      import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
      import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
      import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
      import org.apache.hc.core5.http.ContentType;
      import org.apache.hc.core5.http2.HttpVersionPolicy;
      
      public class A {
          public static void main(String[] args) throws Exception {
              CloseableHttpAsyncClient httpClient = HttpAsyncClientBuilder.create().useSystemProperties()
                  .setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2)
                  .build();
      
              httpClient.start();
      
              SimpleHttpRequest request = SimpleRequestBuilder.post("https://httpbin.org/anything")
                  .setBody("{\"msg\": \"hello\"}".getBytes(), ContentType.APPLICATION_JSON)
                  .build();
      
              SimpleHttpResponse response = httpClient.execute(request, null).get();
              System.out.println(response.getBodyText());
          }
      }

      According to the response, HttpClient doesn't set content-length automatically:

      {
        "args": {}, 
        "data": "{\"msg\": \"hello\"}", 
        "files": {}, 
        "form": {}, 
        "headers": {
          "Content-Type": "application/json; charset=UTF-8", 
          "Host": "httpbin.org", 
          "Transfer-Encoding": "chunked", 
          "User-Agent": "Apache-HttpAsyncClient/5.2-beta1 (Java/17)", 
          "X-Amzn-Trace-Id": "Root=1-636cd35b-506ee990624aebd74fce87b9"
        }, 
        "json": {
          "msg": "hello"
        }, 
        "method": "POST", 
        "origin": "114.246.97.66", 
        "url": "https://httpbin.org/anything"
      }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            liuyb@yahoo-inc.com Yubao Liu
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: