The Crystal Programming Language Forum

How to make a HTTP::Client connect to a unix socket?

I am trying to query the docker engine via /var/run/docker.sock

Not quite sure how to set up HTTP::Client with a unix socket.

I see a bunch of old shards using HTTP::Client.unix but that seems to be obsolete.

Can anyone point me in the right direction?

I naively tried:

require "uri"
require "http/client"

uri = URI.parse "unix:///var/run/docker.sock"
client = HTTP::Client.new uri

puts client

But get a

Unhandled exception: Unsupported scheme: unix

Try creating a https://crystal-lang.org/api/UNIXSocket.html, then passing that IO to HTTP::Client.new.

Ref: Allow HTTP::Client to work with any IO by waj · Pull Request #9543 · crystal-lang/crystal · GitHub

Hello,

It should work:

socket = UNIXSocket.new("/var/run/docker.sock")
client = HTTP::Client.new(socket)

This Docker client seems maintened: GitHub - marghidanu/docr: Crystal Docker client

1 Like