Comments (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.
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)
- Migrating to Spring Boot v2 to v3 and Getting ClassNotFoundException for javax.xml.bind.annotation.XmlElement HOT 1
- AbstractPlatformTransactionManager cannot be proxied with cglib HOT 2
- SSE: potential deadlocks of async tasks threads on connection closed
- Path variable values missing in RedirectView when PathPattern are used
- Part.name is not used with REST HTTP Interface HOT 1
- PathMatchingResourcePatternResolver no longer follows symlinks
- Add constant for 'Alt-Svc' to org.springframework.http.HttpHeaders HOT 1
- Correcting `capitalize` method or naming HOT 1
- Avoid buffering all elements before sending JSON when using Flux/Flow + Kotlinx Serialization Encoder
- Unwrap CGLIB proxy when invoking non-proxied methods in `ReflectionTestUtils` HOT 4
- Issues with Spring jar version spring-web 6.1.12.jar issue with HTTP POST method and Payload is passed as null HOT 1
- spring jms shutdown frizzing application context
- alogrothm
- Updates to resource handling for functional endpoints
- The http call of a Webflux controller method cancelled but the inside Async method call not cancelled - still running. HOT 1
- Can't connect to database postgresql on railway. HOT 1
- Fix a typo in the CDS documentation
- Add TaskDecorator for spring docs
- Race condition in error handling for ClientAbortException within a StreamingResponseBody on Tomcat
- Revert removal of deprecated rawStatusCode methods
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-framework.