Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.58k stars 591 forks source link

Support HttpRequestWithBody#body(InputStream) method #411

Closed dzikoysk closed 2 years ago

dzikoysk commented 2 years ago

Is your feature request related to a problem? Please describe.

I'd like to send large files, especially in tests, to check if server is able to large handle incoming data, e.g. with:

RandomAccessFile hugeFile = new RandomAccessFile(new File(temptDirectory, "test.binary"), "rw")
hugeFile.setLength(1024 * 1024 * 1024) // 1GB
new InputStreamEntity(Channels.newInputStream(hugeFile.getChannel())) // :(

Describe the solution you'd like

Just a simple HttpRequestWithBody#body(InputStream) would be great. It was even referenced in the past as a possible feature, but looks like no one finally did this:

It shouldn't be that hard, as InputStream is already implemented in internals as one of available BodyPart, just not exposed as an public API.

Describe alternatives you've considered

Just using another http client for such a case, as literally every client I know support such an option, e.g. Apache's HttpClient:

def put = new HttpPut(url("/large.binary").toString())
put.setEntity(new InputStreamEntity(Channels.newInputStream(hugeFile.getChannel())))
def response = client.execute(put)
ryber commented 2 years ago

This IS actually supported (sort of) as a named param file https://github.com/Kong/unirest-java/blob/main/unirest/src/test/java/BehaviorTests/MultiPartFormPostingTest.java#L64

It's true there is not a method as a raw body octet stream (not a named file)

dzikoysk commented 2 years ago

Yeah, but parametrized files are a part of forms to upload multiple files, it's slightly different case than just a huge body of the request itself I guess 🤔 Tbh it's not only related to files, but it was easier for me to show it on an example I'm currently working on. Such an API is in general useful to handle InputStream requests that may come from various different sources.

ryber commented 2 years ago

this is released in 3.13.0. Try it out and let me know what you think. You can also add a progress monitor to it (though it doesn't know the total size of the stream of course, so you just get the bits written and the total of bytes written on each chunk.

dzikoysk commented 2 years ago

Works like a charm for me now :)