Affects: \<6.1.0>
Request Method: POST
Request Header Accept: /, application/json Accept-Encoding: gzip, deflate, br Accept-Language: en,zh-CN;q=0.9,zh;q=0.8 Connection: keep-alive Content-Length: 11 Content-Type: application/x-www-form-urlencoded Cookie: JSESSIONID=*** Host: localhost:33000 X-Requested-With: XMLHttpRequest
Form Data: Hostname: ''
API defined as below:
package com.test
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
public class GenericRequestResource {
@SuppressWarnings("rawtypes")
@RequestMapping(value = "/**", method={RequestMethod.GET, RequestMethod.POST,RequestMethod.PUT,RequestMethod.DELETE})
public ResponseEntity handleRequest(@RequestHeader HttpHeaders headers,HttpServletRequest request,HttpServletResponse response,@RequestBody(required=false) String payload){
// method body
}
}
Expected result API call succeed, payload value is "Hostname=%27%27"
Actual Result API call succeed, payload value is "Hostname=%2"
Comment From: JillSAP
same codes run in spring 6.0.13, it work fine.
Compare spring 6.0.13 vs 6.1.2, 6.1.2 will cut the body by content length
6.1.2 org.springframework.http.converter.StringHttpMessageConverter#readInternal
@Override
protected String readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage) throws IOException {
Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType());
long length = inputMessage.getHeaders().getContentLength();
byte[] bytes = (length >= 0 && length <= Integer.MAX_VALUE ?
inputMessage.getBody().readNBytes((int) length) :
inputMessage.getBody().readAllBytes());
return new String(bytes, charset);
}
6.0.13
@Override
protected String readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage) throws IOException {
Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType());
return StreamUtils.copyToString(inputMessage.getBody(), charset);
}
Comment From: snicoll
@JillSAP Did you review the upgrade instructions and, in particular, this section.
To reduce memory usage in RestClient and RestTemplate, most ClientHttpRequestFactory implementations no longer buffer request bodies before sending them to the server. As a result, for certain content types such as JSON, the contents size is no longer known, and a Content-Length header is no longer set. If you would like to buffer request bodies like before, simply wrap the ClientHttpRequestFactory you are using in a BufferingClientHttpRequestFactory.
If that doesn't help, please share a small sample we can run ourselves as it's not very clear from the code snippet what else would be happening.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.