# HTTP.jl: How to post a series of json strings over a live connection

**URL:** <https://discourse.julialang.org/t/http-jl-how-to-post-a-series-of-json-strings-over-a-live-connection/85572>\
**Category:** Web Stack\
**Tags:** question, web, httpjl\
**Created:** [August 10, 2022, 8:20am UTC](https://discourse.julialang.org/t/http-jl-how-to-post-a-series-of-json-strings-over-a-live-connection/85572 "2022-08-10T08:20:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mst](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@mst](https://discourse.julialang.org/u/mst)\
**Post date:** [August 10, 2022, 8:20am UTC](https://discourse.julialang.org/t/http-jl-how-to-post-a-series-of-json-strings-over-a-live-connection/85572/1 "2022-08-10T08:20:50Z")

</div>

Hello,

I have a vector `v` where each element is a valid json string:

```julia
v = [json1, json2, ..., jsonN]

```

using HTTP.jl, I open a connection to a url and post each json in `v`:

```julia
for json ∈ v
  HTTP.post(url, headers, json)
end

```

which seems inefficient to me as (I think) the cycle is:

1. open connection
2. send json string
3. close connection

I suppose a more efficient way is to

1. open a connection (and keep it open)
2. iteratively send each json in `v` to that live connection
3. close the connection

saving all the middle “opening” and “closing” steps – but I don’t know how to  
do it or even if it’s possible.

I’ve tried reading the HTTP.jl documentation, but I don’t know enough of  
networking to figure out how to achieve this.

If anyone knows how to do this, please help!

Thanks.

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [August 10, 2022, 11:44am UTC](https://discourse.julialang.org/t/http-jl-how-to-post-a-series-of-json-strings-over-a-live-connection/85572/2 "2022-08-10T11:44:48Z")

</div>

> - open a connection (and keep it open)

I don’t think that’s possible in HTTP - it’s intentionally a stateless protocol, and doesn’t have persistent connection-sessions.

The first request will populate whatever caches can be persisted (eg. DNS), and some state will be preserved that way which will be useful for the further requests. But that’s from the OS - from your side, sending individual POST requests seems fine to me.

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [August 10, 2022, 12:26pm UTC](https://discourse.julialang.org/t/http-jl-how-to-post-a-series-of-json-strings-over-a-live-connection/85572/3 "2022-08-10T12:26:04Z")

</div>

This really depends on the server you’re hitting. There is a `Transfer-Encoding: chunked`, which relies on sending “chunks” of data over an open connection. For HTTP.jl, if you pass your vector of json strings as the request body, it will automatically do this transfer encoding. But the server you’re making the request to has to be set up to support/handle that.

Alternatively, the websockets protocol is often used for things like this, since you establish a connection and then have bidirectional send/receive for the life of the connection. HTTP.jl provides the `WebSockets` module with that functionality. But again, you need a server on the other end who is ready to make/accept websocket connections of this type.
