-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Add ability to use InputStream as source of RequestBody #1038
Conversation
@@ -98,4 +99,31 @@ public static RequestBody create(final MediaType contentType, final File file) { | |||
} | |||
}; | |||
} | |||
|
|||
/** Returns a new request body that transmits the content of an {@code InputStream}. */ | |||
public static RequestBody create(final MediaType contentType, final InputStream is, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: rename is
to inputStream
(that way the parameter name in your exception lines up with the parameter name in the declaration)
Neat. I need a CLA https://github.com/square/okhttp/blob/master/CONTRIBUTING.md |
Agreement is signed. I will make the changes asap. Please stay tuned. |
Makes sure the InputStream is reset after use so that the stream can be read again if supported.
The build became stale on some unrelated test. Is there anything i can do about that? |
It looks like the retry mechanism gets in the way of using an InputStream. Edit: I just saw that there is indeed a reset in the writeTo finally clause. However, android contentproviders will for some reason give you non-resetable inputstreams, in which case I guess the next call to writeTo will crash |
Sorry for the delay looking at this. Because we can call the method multiple times, I don't think we can accept a raw input stream. Instead the implementation should look up a new input stream each time. Mark/reset doesn't really fix the problem because those methods cause the entire stream body to be buffered, which is quite inefficient. |
Currently you can only use static resources when creating a RequestBody. In practice it can be beneficial to provide an InputStream as the RequestBody data source. For example an input stream can be monitored during the upload so that a use can be provided with feedback about the upload progress.