Posting from an InputStream

102 views
Skip to first unread message

Paul Butcher

unread,
Nov 9, 2011, 11:14:26 AM11/9/11
to dispatc...@googlegroups.com
I've just discovered Dispatch, and am loving it - thank you very much for making it available :-)

I'm writing a little utility that downloads a file from a server and the re-uploads it to a different location. I've got it working just fine by using a temporary file, along the following lines:

> val zip = File.createTempFile("promote", null)
> zip.deleteOnExit
> Http(source >>> new FileOutputStream(zip))
> Http(destination <<< (zip, "application/zip") >|)

But the temporary file bugs me - the amount of data is small and will easily fit in RAM, so I'd like to do something like:

> val data = new ByteArrayOutputStream
> Http(source >>> data)
> Http(destination <<< new ByteArrayInputStream(data.toByteArray) >|)

But I can't find anything that will post from an InputStream :-(

Am I missing something? Is there a better way to achieve the same effect?

--
paul.butcher->msgCount++

Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?

http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: pa...@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher

Nathan Hamblen

unread,
Nov 9, 2011, 3:48:57 PM11/9/11
to dispatc...@googlegroups.com
On 11/09/2011 11:14 AM, Paul Butcher wrote:
> I've just discovered Dispatch, and am loving it - thank you very much for making it available

Hi Paul, glad you like it!

> [...]


>
>> val zip = File.createTempFile("promote", null)
>> zip.deleteOnExit
>> Http(source>>> new FileOutputStream(zip))
>> Http(destination<<< (zip, "application/zip")>|)
> But the temporary file bugs me - the amount of data is small and will easily fit in RAM, so I'd like to do something like:
>
>> val data = new ByteArrayOutputStream
>> Http(source>>> data)
>> Http(destination<<< new ByteArrayInputStream(data.toByteArray)>|)
> But I can't find anything that will post from an InputStream :-(

You mean to PUT from an InputStream, I think, since the <<< method is a
PUT. There isn't support for this built into Dispatch, though there
probably should be. Look at the implementation for the method you are using:

/** PUT the given file. */
def <<< (file: java.io.File, content_type: String) = PUT.copy(
body=Some(new org.apache.http.entity.FileEntity(file, content_type))
)

https://github.com/dispatch/dispatch/blob/master/core/src/main/scala/requests.scala#L153

Instead of FileEntity there are a couple of AbstractHttpEntity
subclasses that would do what you want:

https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/entity/AbstractHttpEntity.html

So you could do this with your own utility method, or you could add it
to Dispatch and send a pull request.

Nathan

Daniel Westheide

unread,
Nov 9, 2011, 4:26:09 PM11/9/11
to dispatc...@googlegroups.com
Hi Paul,

as Nathan already wrote, Dispatch doesn't currently support to PUT or POST from an input stream or byte array. But adding this is really simple and, for the byte arrays, looks pretty much like this:


I agree that both PUT and POST from input streams as well as byte arrays should probably be added to the core library.

Daniel

Paul Butcher

unread,
Nov 10, 2011, 8:12:25 AM11/10/11
to dispatc...@googlegroups.com
On 9 Nov 2011, at 20:48, Nathan Hamblen wrote:
> You mean to PUT from an InputStream, I think, since the <<< method is a PUT.

Of course I do - apologies for any confusion.

> So you could do this with your own utility method, or you could add it to Dispatch and send a pull request.


Many thanks (and to Daniel). If I get a chance to turn this into something I'd be happy to issue a pull request for, I will do - but don't hold your breath (I have my hands full with ScalaMock at the moment :-)

Thanks again!

Gilad Hoch

unread,
Nov 4, 2014, 3:38:53 AM11/4/14
to dispatc...@googlegroups.com
I find it more convenient to use EntityWriter. here's a gist demonstrating my workaround:  https://gist.github.com/hochgi/36e789c71f32caed5a86
Reply all
Reply to author
Forward
0 new messages