Skip to content

RestTemplate with InputStreamResource does not work if Content-Length is not set [SPR-12017] #16633

Closed
@spring-projects-issues

Description

@spring-projects-issues

John Mark opened SPR-12017 and commented

When using an InputStreamResource to upload a file for example

...
MultiValueMap<String, Object> postData = new LinkedMultiValueMap<>();
postData.add("param", "something");
postData.add("file", new InputStreamResource(in));

restTemplate.postForObject(url, postData, String.class);

This causes a strange exception to be thrown: IllegalStateException: InputStream has already been read....

Looking through the code it appears that what is occurring is when AbstractResource.contentLength() is called, it actually reads the entire InputStream. This makes it impossible to use InputStreamResource without defining the Content-Length header. It would be nice if this were not the case. Or if this is required for some reason, at least it should be documented so other people don't have to waste time debugging like I had to.


Affects: 3.2.10, 4.0.6

Issue Links:

Referenced from: commits e14aad8, 5342d92, f0bcb77

Backported to: 3.2.11

Metadata

Metadata

Assignees

Labels

status: backportedAn issue that has been backported to maintenance branchestype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions