Skip to content

restTemplate.exchange have exception "insufficient data written" with springboot 3.3 #33015

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
automvc opened this issue Jun 12, 2024 · 10 comments
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket)

Comments

@automvc
Copy link

automvc commented Jun 12, 2024

The example use springboot 3.0 is normal, but use springboot 3.3 have exception.
Maybe it is different from #31902

detail error:
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://someurl/api/": insufficient data written
at org.springframework.web.client.RestTemplate.createResourceAccessException(RestTemplate.java:915)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:895)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:830)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:691)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 12, 2024
@automvc
Copy link
Author

automvc commented Jun 12, 2024

	try {
		URI uri = getUri(request, method);

		HttpHeaders headers = new HttpHeaders();
		Enumeration<String> headerNames = request.getHeaderNames();
		//set head from request 
		HttpEntity httpEntity = new HttpEntity<>(body, headers);
		ClientHttpRequestFactory clientHttpRequestFactory = (new SimpleClientHttpRequestFactory() {
		     //... 
		});

		ClientHttpRequestFactory factory = new BufferingClientHttpRequestFactory(clientHttpRequestFactory); // already use BufferingClientHttpRequestFactory

		RestTemplate restTemplate = new RestTemplate(factory);
		ResponseEntity serverResponse = null;

		serverResponse = restTemplate.exchange(uri, method, httpEntity, String.class); //this line have ResourceAccessException : insufficient data written

		// ... other
	} catch (Exception e) {
		logger.error("", e);
	}
	return ResponseEntity.badRequest().build();

@poutsma
Copy link
Contributor

poutsma commented Jun 12, 2024

If you'd like us to spend some time investigating, please take the time to provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.

@snicoll
Copy link
Member

snicoll commented Jun 12, 2024

@automvc please stop creating duplicate issues. Can you provide the sample that we requested?

@spring-projects-issues
Copy link
Collaborator

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.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Jun 19, 2024
@spring-projects-issues
Copy link
Collaborator

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.

@spring-projects-issues spring-projects-issues closed this as not planned Won't fix, can't repro, duplicate, stale Jun 26, 2024
@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 26, 2024
@rajukosna
Copy link

Sample code is like:

@Autowired
RestTemplate restTemplate;

try {
URI uri = getUri(request, method);

	HttpHeaders headers = new HttpHeaders();
	Enumeration<String> headerNames = request.getHeaderNames();
	//set head from request 
	HttpEntity httpEntity = new HttpEntity<>(body, headers);
	
	ResponseEntity serverResponse = null;

	serverResponse = restTemplate.exchange(uri, method, httpEntity, String.class); //this line have ResourceAccessException : insufficient data written

	// ... other
} catch (Exception e) {
	logger.error("", e);
}

@ericdriggs
Copy link

ericdriggs commented Apr 9, 2025

Additional information from me duplicating on a service under test:

Exception is being thrown here:
SimpleClientHttpRequest::executeInternal#L88

this.connection.getResponseCode(); //throws java.io.IOException ("Incomplete output stream")

after the connection.connect(), the body and status code are empty

response works fine using curl/postman

errors with all of the following factory attempts

  • new RestTemplate()
  • ClientHttpRequestFactory factory =
    new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory());
    RestTemplate restTemplate = new RestTemplate(factory);
  • HttpComponentsClientHttpRequestFactory (returns different 500)

spring boot version 3.4

Response information:

Content-Type: application/json
Content-Length: 219
status: 400
body: {"error": {"status": "400 Bad Request", "message": "..."}}
(Content length verified to be correct using postman)

@bclozel
Copy link
Member

bclozel commented Apr 10, 2025

@ericdriggs unfortunately this doesn't help much. We can revisit this if you open a new issue with a minimal sample application we can run that reproduces the problem.

@gearkim0614
Copy link

restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory()); fixed it for me

@polatgo1
Copy link

polatgo1 commented May 30, 2025

@gearkim0614 suggestion fixes "insufficient data" case. But this causes redirection issue. It throws 421 when there is redirection to a public website, for example google.com, because http/2 is forced on background. This is so annoying that there isn't currently a solution that fixes both redirection and insufficent data issues.

result is always same ->>>>>> 421 Misdirected Request on GET request for ....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket)
Projects
None yet
Development

No branches or pull requests

9 participants