What sort of message does Snapd's API expect?

95 Views Asked by At

Snapd has documentation on a REST API.

I'm able to connect to the socket from C# using the following

var snapSocket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
var snapEndpoint = new UnixEndPoint("/run/snapd.socket");
snapSocket.Connect(snapEndpoint);
var req = Encoding.ASCII.GetBytes("GET /v2/system-info HTTP/1.1");
snapSocket.Send(req, req.Length, 0);
var bytesReceived = new byte[256];
var bytes = 0;
var response = "";
do
{
    bytes = snapSocket.Receive(bytesReceived, bytesReceived.Length, 0);
    response = response + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
} while (bytes > 0);
Console.WriteLine(response);

But everything halts at snapSocket.Receive - a response is never sent. I suspect that there's something wrong with the message that I'm sending it.

2

There are 2 best solutions below

0
On BEST ANSWER

It turns out that it expects a standard HTTP request, which means a Host: line, a Connection: Close line, and two \ns at the very end are required.

The documentation's following claim...

While it is expected to allow clients to connect using HTTPS over a TCP socket, at this point only a UNIX socket is supported.

... is meant only to imply that HTTPS and TCP do not work yet - HTTP is currently the valid request format even when using the UNIX Socket.

0
On

I am not fluent in C# at all, but maybe this python snippet can help lead into a solution:

import requests_unixsocket
session = requests_unixsocket.Session()
r = session.get('http+unix://%2Frun%2Fsnapd.socket/v2/snaps')
r.raise_for_status()
r.json()