[Feature Request] - RequestBody supports InputStream
See original GitHub issueI would like to submit a PR to add support for InputStream
s in the RequestBody
class. As of now, RequestBody
supports the following:
create(MediaType contentType, String content)
create(MediaType contentType, ByteString content)
create(MediaType contentType, byte[] content)
create(MediaType contentType, byte[] content, final int offset, final int byteCount)
create(MediaType contentType, File file)
I would like to go one step further and add support for InputStream
:
create(MediaType contentType, Inputream inputStream)
Similar to https://stackoverflow.com/a/25384793:
public static RequestBody create(final @Nullable MediaType contentType, final InputStream inputStream) {
if (inputStream == null) throw new NullPointerException("inputStream == null");
return new RequestBody() {
@Override public @Nullable MediaType contentType() {
return contentType;
}
@Override public long contentLength() {
return inputStream.available() == 0 ? -1 : inputStream.available();
}
@Override public void writeTo(BufferedSink sink) throws IOException {
Source source = null;
try {
source = Okio.source(inputStream);
sink.writeAll(source);
} finally {
Util.closeQuietly(source);
}
}
};
}
This would be nice to have since Okio.source
support both File
and InputStream
.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:13 (9 by maintainers)
Top Results From Across the Web
Why RequestBody in HttpServletRequest is an InputStream?
Using an InputStream gives freedom to the servlet implementation to source the request body value from wherever it likes.
Read more >RequestBody (AWS SDK for Java - 2.18.38) - Amazon AWS
Represents the body of an HTTP request. Must be provided for operations that have a streaming input. Offers various convenience factory methods from...
Read more >How can one access raw content of the request stream?
I would very much like to have access to the underlying raw inputstream. I have found a couple posts that ... Sending the...
Read more >File Upload - Swagger
Swagger 2.0 supports file upload requests with Content-Type: multipart/form-data , but does not care about the HTTP method. You can use POST, PUT...
Read more >Web on Servlet Stack - Spring
All HandlerMapping implementations support handler interceptors that are useful when you want to apply specific functionality to certain requests — for example, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@JakeWharton I know it is not an Android library. I figured after the first comment, I would have to keep this code locally and not be able to make a PR. Something like this:
Based on your changes, @JakeWharton, to be changed to handle
Uri
:@neiljaywarner
https://github.com/Miha-x64/Lychee#http