httphttp-postmultipartform-datareactor-netty

Reactor httpclient sendForm with Content-Length


I'm learning reactor-netty as http client to send multi form data. My Server requires Content-Length field but seems reactor-netty doesn't set it automatically.(Interestingly it sets Content-Length in each part, which doesn't satisfy my server)

HttpClient.create().port(server.port()).post().uri("/postpath")
        .sendForm((req, form) -> form
            .multipart(true)
            .file("a", new ByteArrayInputStream(jsona.getBytes(StandardCharsets.UTF_8)), "application/json")
            .file("b", new ByteArrayInputStream(jsonb.getBytes(StandardCharsets.UTF_8)), "application/json")
        )
        //...omitted, same as the github examples
Http traffic is like following:
POST /post HTTP/1.1
user-agent: ReactorNetty/1.1.4
host: myservernmae
accept: */*
content-type: application/json
content-type: multipart/form-data; boundary=40e3ae2412eea9a2
transfer-encoding: chunked

--40e3ae2412eea9a2
content-disposition: form-data; name="a"
content-length: 94
content-type: application/json
content-transfer-encoding: binary

dataa...
--40e3ae2412eea9a2
content-disposition: form-data; name="b"
content-length: 205
content-type: application/json
content-transfer-encoding: binary

datab...
--40e3ae2412eea9a2--

Compared to CloseableHttpClient in http apache client

      final HttpPost httppost = new HttpPost("http://myservernmae/postpath");
      final HttpEntity reqEntity = MultipartEntityBuilder.create()
          .addPart("a", aBody)
          .addPart("b", bBody)
          .build();
      httppost.setEntity(reqEntity);
      final CloseableHttpClient httpclient = HttpClients.createDefault();
      httpclient.execute(httppost, response -> {
      // omit remaining
POST /post HTTP/1.1
Accept-Encoding: gzip, x-gzip, deflate
Content-Length: 611
Content-Type: multipart/form-data; charset=ISO-8859-1; boundary=dbiD5AN62qhyG8C8HAbOPu5zlltTOSu2q
Host: myservernmae
Connection: keep-alive
User-Agent: Apache-HttpClient/5.2.1 (Java/11.0.12)

--dbiD5AN62qhyG8C8HAbOPu5zlltTOSu2q
Content-Disposition: form-data; name="a"
Content-Type: application/json; charset=UTF-8

dataa...
--dbiD5AN62qhyG8C8HAbOPu5zlltTOSu2q
Content-Disposition: form-data; name="b"
Content-Type: application/json; charset=UTF-8

datab...
--dbiD5AN62qhyG8C8HAbOPu5zlltTOSu2q--

Could anyone tell me how to set the Content-Length? better automatically. I'm using jdk11, reactor-netty 1.1.4.


Solution

  • By default, Reactor Netty adds Transfer-Encoding: chunked. Just remove this header before composing your request and Reactor Netty will add Content-Length with a value summing the lengths of all parts. See below the modified example:

        HttpClient.create().port(server.port()).post().uri("/postpath")
                .sendForm((req, form) -> {
                    req.requestHeaders().remove(HttpHeaderNames.TRANSFER_ENCODING);
    
                    form.multipart(true)
                        .file("a", new ByteArrayInputStream(jsona.getBytes(StandardCharsets.UTF_8)), "application/json")
                        .file("b", new ByteArrayInputStream(jsonb.getBytes(StandardCharsets.UTF_8)), "application/json")
                })
                //...omitted, same as the github examples