Curl Access for CaosDB

As the API that is used to communicate with the CaosDB server is XML over HTTP also simple HTTP clients, such as cURL can be used. cURL is an old and established command line program for transferring files over networks that implements various protocols including HTTP/HTTPS. It is installed by default on many Linux distributions and can therefore be very useful for testing and debugging of CaosDB.

This small manual also gives some practical insights about the CaosDB protocol itself.

Doing a simple retrieve

So, let’s start right away with a few basic examples.

Let’s do a query on our demo instance:

curl ""

By default cURL sends an HTTP GET request which is needed for doing queries for CaosDB. CaosDB requires sending them to /Entity. The query itself is specified after the HTTP query string ?query=. %20 is specific to URL encoding and corresponds to a space (see for details). So the actual query we were doing was FIND Experiment which should return all entities with name or with parent Experiment.

The response should look like:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="" ?>
<Response srid="b56e3d1a442460c46dde924a54e8afba" timestamp="1561453363382" baseuri="" count="3">
  <Query string="FIND Experiment" results="3">
    <ParseTree>(cq FIND (entity Experiment) &lt;EOF&gt;)</ParseTree>
    <Role />
    <TransactionBenchmark since="Tue Jun 25 11:02:43 CEST 2019" />
  <RecordType id="212" name="Experiment">
    <Permissions />
  <RecordType id="215" name="UnicornExperiment">
    <Permissions />
    <Parent id="212" name="Experiment" />
  <Record id="230">
    <Permissions />
    <Parent id="215" name="UnicornExperiment" />
    <Property id="216" name="date" datatype="DATETIME" importance="FIX">
      <Permissions />
    <Property id="217" name="species" datatype="TEXT" importance="FIX">
      <Permissions />
    <Property id="214" name="Conductor" datatype="TEXT" importance="FIX">
      Anton Beta
      <Permissions />
    <Property id="221" name="Photo" datatype="Photo" importance="FIX">
      <Permissions />
    <Property id="220" name="LabNotes" datatype="LabNotes" importance="FIX">
      <Permissions />
    <Property id="218" name="UnicornVideo" datatype="UnicornVideo" importance="FIX">
      <Permissions />
    <Property id="219" name="UnicornECG" datatype="UnicornECG" importance="FIX">
      <Permissions />

We can see, that in fact two RecordTypes and one Record are returned. Furthermore the response contains some additional information:

  • Attributes in the Response tag:

    • srid="b56e3d1a442460c46dde924a54e8afba" A unique identifier for this request.

    • timestamp="1561453363382" The UNIX timestamp for this request.

    • baseuri="" The base URI of the instance of CaosDB that performed the request.

    • count="3" The number of results (2 RecordTypes and 1 Record)

  • Information about the user in UserInfo. In this case we have not logged in, so we are anonymous.

  • Detailled information about the query. This includes for example the parse tree and can be used for debugging and testing. Depending on the settings of the server instance, this tag includes more or less detail, for example a more detailled transaction benchmark.

More details about the retrieve

The cURL statement used in the previous section made use of a lot of default settings for cURL. Let’s have a closer look behind the options. (I assigned the above URL to a shell variable, to make the statement more readable.)


curl -X GET -b cookie.txt -D head.txt $URL

This command specifies three more options:

  • -X GET Do a GET request. This can of course be replaced by POST, PUT, DELETE or any other HTTP operation.

  • -b cookie.txt This instructs cURL to use cookies from the file cookie.txt (which we don’t have yet, see below)

  • -D head.txt Tell cURL to store the received header in the file head.txt.

Running this command will give us a similar response than in the previous section, but additionally a file head.txt:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=UTF-8
Date: Tue, 25 Jun 2019 09:17:23 GMT
Accept-Ranges: bytes
Server: Restlet-Framework/2.3.12
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Transfer-Encoding: chunked

There is nothing special in that header. Most importantly the request has lead to a response without HTTP error.

Logging in

You might have asked yourself, what we need the cookie for. The simple answer is: authentication. For many operations done on the CaosDB server we have to log in first. The demo instance is configured to allow for anonymous read access by default. But depending on the instance you are accessing, even this might be disallowed.

You can log in to the server using cURL with the following command:


curl -X POST -c cookie.txt -D head.txt -H "Content-Type: application/x-www-form-urlencoded" -d username=salexan -d password=$PW $URL

So here we are doing a POST request instead of a GET. We are instructing cURL to use the cookie.txt file to store the cookies it recieves. This is done using the -c option (instead of the -b option above). This time we are explicitely specifying the content type of the content we are sending (using the POST request) with the -H option. The actual content sent are the two fields specified using the -d options. This boils down to the two key-value-pairs “username=” and “password=”.

This time we are sending the information to a different context indentified by /login.

If you don’t want to supply your passowrd in plain text you can for example use a password manager (like I do), in my case pass as follows to store your password in the variable $PW:

PW=$(pass your/passowrd/identifier)

Custom Certificates

If you are running your own CaosDB instance it might be necessary to use a custom SSL certificate.

You can specify this using the option --cacert, e.g.:

--cacert "/path/to/certificate/root.cert.pem"

Uploading files

According to the specification, a file upload is a POST with multipart form data. This can be achieved using CURL with the following simple command line:

curl -b cookie.txt \
     -F "FileRepresentation=<file.xml" \
     -F "testfile.bla=@testfile.bla" \

Here I am using a previously stored cookie from cookie.txt.

There are two prerequisites to executing the above command: - The file representation file.xml which is in this case stored in a file. - The actual file to be uploaded with name testfile.bla. The left hand side of the assignment is very important as the identifier given here (which in this case is testfile.bla) will be used in the XML for identifying the file that is described in the corresponding XML tag.

Let’s have a look at the contents of file.xml:

  <File upload="testfile.bla" destination="/test/testfile.bla" description="bla"

The attributes have the following meaning: - upload=testfile.bla The filename given here is actually no filename, but an identifier to find the multipart data chunk that contains the file. I called it testfile.bla for simplicity here. - destination="/test/testfile.bla" The destination path on the CaosDB server file system.

Before looking at the other attributes let’s have a look at the file testfile.bla itself:


The file has size “3” which can be verified on linux using a:

stat testfile.bla

It’s hashsum is important for checking the integrity after the transfer to the server. It can be computed on linux using:

sha512sum testfile.bla

These information has to be supplied as the remaining attributes to the XML.