Coder Social home page Coder Social logo

Comments (10)

mdeinum avatar mdeinum commented on June 12, 2024 2

The Spring RestClient is nothing more then a wrapper, however internally it will use a Streaming Request and not a plain String or other simple type.

Rewriting the code to use something streaming makes it fail with a plain HttpClient as well.

var client = HttpClient.newHttpClient();
var body = new ObjectMapper().writeValueAsBytes(new SomeRequest(16));
var publisher = HttpRequest.BodyPublishers.ofByteArray(body, 0, body.length);
var request = HttpRequest.newBuilder().
  .uri(URI.create(runtimeInfo.getHttpBaseUrl() + "/something"))
  .header("Content-Type", "application/json")
  .PUT(HttpRequest.BodyPublishers.fromPublisher(publisher))
  .build();

var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());

Result: java.io.IOException: EOF reached while reading

I've attached the logging for the HttpClient maybe that can shed some light. The output for both the RestClient or plain HttpClient is the same as well as the end result.

logging-when-eof.txt

UPDATE: Fails with a simple byte[] as well in the exact same manner.

from wiremock.

nut-3 avatar nut-3 commented on June 12, 2024

If I'm not mistaken there are no implementations of HTTP/2 without TLS. So my guess is that you set up a http stub that is served with HTTP/1.1. So it might be Java HttpClient's bug for not downgrading to HTTP/1.1 on non-TLS connection or not handling failed upgrade attempt properly. Or maybe underlying Jetty is not configured to use h2c for not-TLS HTTP/2 connections (probably need some configuration to enable it).
Actually, your code can be changed to use only one ClientHttpRequestFactory:

        return RestClient.builder()
                .requestFactory(new JdkClientHttpRequestFactory(HttpClient.newBuilder()
                        .version(HttpClient.Version.HTTP_1_1)
                        .build()))
                .baseUrl(properties.baseUrl())
                .build();

and it works.

from wiremock.

wimdeblauwe avatar wimdeblauwe commented on June 12, 2024

The point is that Wiremock is that HTTP stub and forcing HTTP/1.1 should not be needed.

from wiremock.

nut-3 avatar nut-3 commented on June 12, 2024

Well, I can only add that I could not reproduce error by using plain Jdk HttpClient like this:

        var httpClient = HttpClient.newHttpClient();
        HttpResponse<String> response = httpClient.send(HttpRequest.newBuilder()
                        .uri(new URI(runtimeInfo.getHttpBaseUrl() + "/something"))
                        .header("Content-Type", "application/json")
                        .PUT(HttpRequest.BodyPublishers.ofString("{ \"data\": \"test\" }"))
                        .build(),
                HttpResponse.BodyHandlers.ofString()
        );

Same using sendAsync(). Maybe error has something to do with Spring RestClient usage of HttpClient.

from wiremock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.