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

Hello,

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

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

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

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.

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

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.

1 Like