# HTTP::Client stream body as IO

**URL:** <https://forum.crystal-lang.org/t/http-client-stream-body-as-io/1496>\
**Category:** Crystal Contrib\
**Created:** [December 16, 2019, 11:11am UTC](https://forum.crystal-lang.org/t/http-client-stream-body-as-io/1496 "2019-12-16T11:11:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![j8r](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/j8r/32/111_2.png) [@j8r](https://forum.crystal-lang.org/u/j8r)\
**Post date:** [December 16, 2019, 11:11am UTC](https://forum.crystal-lang.org/t/http-client-stream-body-as-io/1496/1 "2019-12-16T11:11:12Z")

</div>

How can I stream a body as an IO with `HTTP::Client`, that is sent to a remote server?  
From [what I saw](https://github.com/crystal-lang/crystal/blob/6952aacb37682558d1a976b4ebf1b1456d9f8b84/src/http/common.cr#L255), it is not currently possible - an intermediary IO is copied to the socket IO.

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [December 16, 2019, 12:32pm UTC](https://forum.crystal-lang.org/t/http-client-stream-body-as-io/1496/2 "2019-12-16T12:32:14Z")

</div>

Check out `HTTP::FormData`. I’d link it but I’m on my phone. 🙂 It’s in the stdlib, though. I just discovered it the other day.

---

<div class="post-metadata">

**Author:** ![j8r](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/j8r/32/111_2.png) [@j8r](https://forum.crystal-lang.org/u/j8r)\
**Post date:** [December 16, 2019, 12:38pm UTC](https://forum.crystal-lang.org/t/http-client-stream-body-as-io/1496/3 "2019-12-16T12:38:48Z")

</div>

No, it’s not related. This is for parsing a `multipart/form-data` response, not sending data as an IO stream.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [December 16, 2019, 1:40pm UTC](https://forum.crystal-lang.org/t/http-client-stream-body-as-io/1496/4 "2019-12-16T13:40:22Z")

</div>

What exactly are you looking for, if not `IO.copy`? That does exactly do what you asked for.

---

<div class="post-metadata">

**Author:** ![j8r](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/j8r/32/111_2.png) [@j8r](https://forum.crystal-lang.org/u/j8r)\
**Post date:** [December 16, 2019, 1:44pm UTC](https://forum.crystal-lang.org/t/http-client-stream-body-as-io/1496/5 "2019-12-16T13:44:27Z")

</div>

I was wondering if I can directly write to the socket IO, without any intermediary IO?  
In my case, this is for streaming JSON to a remote server.  
So instead of all JSON -\> IO copy to -\> Socket IO, doing JSON -\> stream to -\> Socket IO.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [December 16, 2019, 3:25pm UTC](https://forum.crystal-lang.org/t/http-client-stream-body-as-io/1496/6 "2019-12-16T15:25:14Z")

</div>

The reason this can’t be provided is because sending a request can either be with a fixed content-length or chunked. In the case of content-length the length must be specified before the body. Maybe you know the length and can stream directly? Maybe. But then if it’s chunked, which is what you probably want, each chunk length comes before the chunk and so it’s basically the same problem. In Go it’s also the same: when you post a reader you provide an IO to read from, then the client reads chunks from it, write the length and then the chunk. It’s a bit impossible to let you do the writing.

---

<div class="post-metadata">

**Author:** ![j8r](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/j8r/32/111_2.png) [@j8r](https://forum.crystal-lang.org/u/j8r)\
**Post date:** [December 16, 2019, 3:27pm UTC](https://forum.crystal-lang.org/t/http-client-stream-body-as-io/1496/7 "2019-12-16T15:27:53Z")

</div>

I see, thank you for the explanation @asterite!  
HTTP isn’t suited for this.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [December 16, 2019, 6:31pm UTC](https://forum.crystal-lang.org/t/http-client-stream-body-as-io/1496/8 "2019-12-16T18:31:18Z")

</div>

You can actually use a pipe in order to have a write IO. It adds a little overhead though.

Technically, I think it should be possible to provide access to the (write) IO directly. When wrapped in a buffered writer, it would be perfectly fine to use a chunked transfer encoding.

---

<div class="post-metadata">

**Author:** ![j8r](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/j8r/32/111_2.png) [@j8r](https://forum.crystal-lang.org/u/j8r)\
**Post date:** [March 14, 2020, 3:32pm UTC](https://forum.crystal-lang.org/t/http-client-stream-body-as-io/1496/9 "2020-03-14T15:32:38Z")

</div>

I’m doing a proxy, transfer a `HTTP::Client::Response` to the `HTTP::Server::Response`, it could benefit from this.
