Skip to content
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

Creating RequestBody from an InputStream #2424

Closed
jianghaolu opened this issue Mar 21, 2016 · 9 comments
Closed

Creating RequestBody from an InputStream #2424

jianghaolu opened this issue Mar 21, 2016 · 9 comments

Comments

@jianghaolu
Copy link

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!

@swankjesse
Copy link
Collaborator

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

@jianghaolu
Copy link
Author

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

@swankjesse
Copy link
Collaborator

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

@jianghaolu
Copy link
Author

Or retries. Makes sense! Thanks.

@Ulanxx
Copy link

Ulanxx commented Jan 17, 2017

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

@bpappin
Copy link

bpappin commented Jul 30, 2018

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.

@JakeWharton
Copy link
Collaborator

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.

@bpappin
Copy link

bpappin commented Jul 30, 2018

@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.

@JakeWharton
Copy link
Collaborator

JakeWharton commented Jul 30, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants