I'm attempting to do a POST with the body being an InputStream with something like this:
@POST("/build")
@Headers("Content-Type: application/tar")
Response build(@Query("t") String tag,
@Query("q") boolean quiet,
@Query("nocache") boolean nocache,
@Body TypedInput inputStream);
In this case the InputStream is from a compressed tar file.
What's the proper way to POST an InputStream?
The only solution I came up with here was to use the TypeFile class:
and the interface (without explicitly setting the Content-Type header this time):
Using my own implementation of TypedInput resulted in a vague EOF exception even while I provided the length().
Also, while troubleshooting this issue I tried using the latest Apache Http client instead of OkHttp which resulted in a "Content-Length header already present" error even though I wasn't explicitly setting that header.
TypedInput
is a wrapper around anInputStream
that has metadata such as length and content type which is used in making the request. All you need to do is provide a class that implementsTypedInput
which passed your input stream.Be sure you pass the appropriate return values for
length()
andmimeType()
based on the type of file from which you are streaming content.You can also optionally pass it as an anonymous implementation when you are calling your
build
method.My solution was to implement
TypedOutput
According to the Multipart section of http://square.github.io/retrofit/ you'll want to use TypedOutput instead of TypedInput. Following their examples for multipart uploads worked fine for me once I had implemented a TypedOutput class.