Skip to content

SimpleClientHttpRequest works different from previous SimpleBufferingClientHttpRequest. Cause of that now get: java.io.IOException: insufficient data written #39037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Jan 5, 2024 · 5 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@ghost
Copy link

ghost commented Jan 5, 2024

Since spring boot 3.2 there are changes for RestTemplate to use new SimpleClientHttpRequest
SimpleClientHttpRequest.executeInternal is different from before SimpleBufferingClientHttpRequest.executeInternal main problem is in logic

this.connection.setFixedLengthStreamingMode

In SimpleClientHttpRequest to setFixedLengthStreamingMode we take from headers content-lenght
In SimpleBufferingClientHttpRequest we used predefined buffer 1024.

Cause of this change always get: java.io.IOException: insufficient data written on post.

Is this bug ? Or expected change ?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 5, 2024
@bclozel
Copy link
Member

bclozel commented Jan 5, 2024

This is a known change, see this section of the upgrade guide:

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.

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Jan 5, 2024
@bclozel bclozel added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 5, 2024
@ghost
Copy link
Author

ghost commented Jan 5, 2024

Hi, @bclozel

Thanks for fast response.

Before writing issue. Checked upgrade guide info and found your described solution it doesn't solve problem at least in my side. It was already tested as guide describe.

new RestTemplateBuilder().requestFactory(() -> new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()))

Problem still persists: java.io.IOException: insufficient data written.
content-lenght: is incorrectly taken and calculated.

Only solution what I found was to calculate and preset correct content-lenght in headers before calling exchange.
Additional i think it's same problem: spring-cloud/spring-cloud-gateway#3154

@bclozel
Copy link
Member

bclozel commented Jan 5, 2024

If your problem is similar to spring-cloud/spring-cloud-gateway#3154, it means you are using RestTemplate as a client for proxying requests and that you are copying the Content-Length header back and that this turns out to be the wrong length. If anything, this means that you should not set the Content-Length header yourself unless you are sure about the actual body length.

@ghost
Copy link
Author

ghost commented Jan 5, 2024

Yes it's so. I'm using it as proxy.

public ResponseEntity post(final String postfix, final Map body, final HttpServletRequest req) {
final HttpHeaders headers = getHeaders(req);
final ResponseEntity response = restTemplate.exchange("url", HttpMethod.POST, new HttpEntity>(body, headers), Object.class);
return getResult(response);
}

At moment i'm forced to do content-lenght calculations in getHeaders. To fix problem which are introduced since spring boot 3.2.

@bclozel
Copy link
Member

bclozel commented Jan 5, 2024

I think you should move this to StackOverflow and get some advice there. Your implementation is probably problematic in many other ways (security headers or other sensitive headers, see spring-projects/spring-framework#21523). This is not a Spring bug and you should review your implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

2 participants