Coder Social home page Coder Social logo

Comments (2)

bclozel avatar bclozel commented on September 2, 2024 2

I've tracked this behavior to #10207. This is because HttpUrlConnection does not support setting a request body for "GET" requests and will turn it into a POST request automatically. In Spring, our request factory will avoid sending the body altogether to avoid turning it into a POST.

See the JDK code in action here: https://github.com/openjdk/jdk/blob/jdk-23%2B25/src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java#L1431-L1433

You can verify that in action without Spring being involved with:

String body = "test body";
URL url = new URL("http://localhost:"+port+"/test");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setFixedLengthStreamingMode(body.length());
con.setDoOutput(true);
con.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON_VALUE);
OutputStream outputStream = con.getOutputStream();
outputStream.write(body.getBytes(StandardCharsets.UTF_8));
outputStream.flush();
outputStream.close();

int responseCode = con.getResponseCode();
assertThat(responseCode).isEqualTo(200);

This will send the request as a POST:

2024-06-04T12:07:37.206+02:00 DEBUG 11214 --- [restclient] [    Test worker] s.n.www.protocol.http.HttpURLConnection  : sun.net.www.MessageHeader@249b54af7 pairs: {POST /test HTTP/1.1: null}{Content-Type: application/json}{User-Agent: Java/17.0.11}{Host: localhost:52192}{Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2}{Connection: keep-alive}{Content-Length: 9}

In this case @ngocnhan-tran1996 , please use a different request factory for your client. You can configure the request factory manually for your entire application or add a library on the classpath that will be picked up instead of the default one.

from spring-framework.

snicoll avatar snicoll commented on September 2, 2024

Thanks for the sample. The underlying ClientHttpRequestFactory are not the same. When you create the client yourself, you get Spring Framework's default, i.e. org.springframework.http.client.JdkClientHttpRequestFactory. If you configure the client using the builder that Spring Boot has auto-configured for you, you get a org.springframework.http.client.SimpleClientHttpRequestFactory.

The former is new in 6.1 and Spring Boot couldn't swap the default behavior for backward compatible reason, see spring-projects/spring-boot#38856 and the issue that it links.

I am not sure why SimpleClientHttpRequestFactory wouldn't write the body. @poutsma, do you know?

from spring-framework.

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.