Skip to content

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

Closed

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

Activity

  1. spring-projects-issues commented on Jul 22, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    The problem here is that with an InputStreamResource, the given InputStream can technically only be read once. It's unfortunate here that AbstractResource's contentLength() implementation performs that one read attempt and therefore renders the InputStreamResource unusable afterwards. I guess we should rather throw an IllegalStateException from an InputStreamResource-specific contentLength() implementation, signalling that the content length cannot be determined without side effects. RestTemplate and other callers can deal with that accordingly then, simply not knowing the content length in advance.

    That said, it's definitely recommendable to use one of the more specific Resource implementations if you can, which among other benefits are usually capable of determining the content length without side effects. InputStreamResource should be a last resort only.

    Juergen

  2. spring-projects-issues commented on Jul 22, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Alternatively, we could also let RestTemplate check for an InputStreamResource and simply not call contentLength() on it in that case, since it knows that the InputStreamResource will be used for full content reading afterwards.

    For other kinds of callers, just determining the contentLenght() might be alright if they never end up reading the actual content afterwards.

    Juergen

  3. spring-projects-issues commented on Jul 22, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    As of 4.1 RC2, ResourceHttpMessageConverter does not call contentLength() on InputStreamResource anymore. To be backported to 4.0.7 and 3.2.11.

    Juergen

  4. added
    status: backported
    type: enhancement
    on Jan 11, 2019
  5. added this to the 4.0.7 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

status: backportedtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions