Title: Making REST calls from Javascript in Cockpit Date: 2015-07-10 Category: Cockpit, Linux Tags: cockpit, linux Slug: making-rest-calls-from-javascript-in-cockpit *Note: This post has been updated for changes in Cockpit 0.90 and later.* [Cockpit is a user interface for servers](http://cockpit-project.org). In [earlier](http://stef.thewalter.net/creating-plugins-for-the-cockpit-user-interface.html) [tutorials](http://stef.thewalter.net/using-dbus-from-javascript-in-cockpit.html) there's a guide on how to add components to Cockpit. Not all of the [system APIs use DBus](http://stef.thewalter.net/d-bus-is-powerful-ipc.html). So sometimes we find ourselves in a situation where we have to use REST (which is often just treated as another word for HTTP) to talk to certain parts of the system. For example [Docker has a REST API](https://docs.docker.com/reference/api/docker_remote_api/). For this tutorial you'll need at least Cockpit 0.58. There was one last tweak that helped with the ```superuser``` option you see below. You can install it in [Fedora 22](http://cockpit-project.org/running.html) or [build it from git](https://github.com/cockpit-project/cockpit/blob/master/HACKING.md). Here we'll make a package called *docker-info* which shows info about the docker daemon. We use the `/info` [docker API](https://docs.docker.com/reference/api/docker_remote_api_v1.18/#display-system-wide-information) to retrieve that info. I've prepared the [docker-info package here](http://stef.thewalter.net/files/docker-info.tgz). It's just two files. To download them and extract to your current directory, and installs it as a Cockpit package: :::text $ wget http://stef.thewalter.net/files/docker-info.tgz -O - | tar -xzf - $ cd docker-info/ $ mkdir -p ~/.local/share/cockpit $ ln -snf $PWD ~/.local/share/cockpit/ Previously we [talked about](http://stef.thewalter.net/creating-plugins-for-the-cockpit-user-interface.html) how packages are installed, and what `manifest.json` does so I won't repeat myself here. But to make sure the above worked correctly, you can run the following command. You should see `docker-info` listed in the output: :::text $ cockpit-bridge --packages ... docker-info: .../.local/share/cockpit/docker-info ... If you're logged into Cockpit on this machine, first log out. And log in again. Make sure to log into Cockpit with your current user name, since you installed the package in your home directory. You should now see a new item in the *Tools* menu called *Docker Info*: ![Docker Info tool](images/cockpit-docker-info.png) After a moment, you should see numbers pop up with some stats about the docker daemon. Now in a terminal try to run something like: :::text $ sudo docker run -ti fedora /bin/bash You should see the numbers update as the container is pulled and started. When you type ```exit``` in the container, you should see the numbers update again. How is this happening? Lets take a look at the `docker-info` HTML: :::html