Okhttp: Creating RequestBody from an InputStream

Created on 21 Mar 2016  Â·  9Comments  Â·  Source: square/okhttp

I was looking at RequestBody.java and I can't find an overload for create() like this:

RequestBody.create(MediaType contentType, InputStream stream)'

Is there a reason not to implement this? This is pretty useful when piping from one stream into an upload job. The overload accepting a File is useful for files but that's not the only scenario where buffering in memory does not work.

Please shed some lights if I'm missing something. Thanks!

Most helpful comment

Course of project , i need the overload , do you find answers?

All 9 comments

In general we want to be able to replay a request body multiple times. But input streams can only be consumed once.

So if I provide a large file it will might be read multiple times?

Yep. For example, if you get a 308 redirect.

Or retries. Makes sense! Thanks.

Course of project , i need the overload , do you find answers?

Handling files on Android is not nice at all, so we need to use content:// URIs.
We can get a streams from them, or File Descriptors, but not a file.

The Uri is what you should create a RequestBody from then. As if it needs to be written twice it can have its stream opened twice.

@JakeWharton yes i agree, but there is no RequestBody.create(MimeType, Uri) that i can see.

The only way I can see to do it is to override RequestBody and source from the file descriptor.

Yep. That's the right way.

On Mon, Jul 30, 2018 at 5:38 PM Brill Pappin notifications@github.com
wrote:

@JakeWharton https://github.com/JakeWharton yes i agree, but there is
no RequestBody.create(MimeType, Uri) that i can see.

The only way I can see to do it is to override RequetBody and source from
the file descriptor.

—
You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/square/okhttp/issues/2424#issuecomment-409020429, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAEEEfH84q_iL-uf7-M4WMfOzMGEROUuks5uL3zkgaJpZM4H1nhR
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SandroMachado picture SandroMachado  Â·  3Comments

vanshg picture vanshg  Â·  3Comments

theotherp picture theotherp  Â·  3Comments

SElab2019 picture SElab2019  Â·  3Comments

yschimke picture yschimke  Â·  3Comments