Lets begin by installing an "internal server". CouchDB is a database that uses the HTTP protocol (no DatabaseLink needed here). It works using REST like many other web services out there. If you are on a Mac or PC it is just a matter of running the installer with default options. After this we can confirm that the server is working:
In[1]:= Import["http://127.0.0.1:5984"]
Out[1]= {"couchdb":"Welcome","version":"2.0.0","vendor":{"name":"The Apache Software Foundation"}}
This server always replies in JSON format so we can fetch and parse in one step:
In[2]:= Import["http://127.0.0.1:5984","RawJSON"]
Out[2]= <|couchdb->Welcome,version->2.0.0,vendor-><|name->The Apache Software Foundation|>|>
I use Import[]
for fetching because it is the easiest way to generate and parse simple GET requests. But you can also use other commands like URLRead
or URLFetch
.
Let's create a database. In CouchDB tutorial, they do it like this:
curl -X PUT http://127.0.0.1:5984/baseball
from Mathematica, one way of doing this is:
In[3]:= URLExecute[HTTPRequest["http://127.0.0.1:5984/mydatabase", <|"Method" -> "PUT"|>]]
Out[3]= {"ok" -> True}
By default, CouchDB allows all operations (admin party) but only from the same computer where it is installed. If you add a user/password then you need to provide your credentials or a cookie.
Now lets get a list of the databases in order to verify that "mydatabase" is there:
In[4]:= Import["http://127.0.0.1:5984/_all_dbs","RawJSON"]
Out[4]= {_global_changes, _metadata, _replicator, _users, mydatabase}
It is there. Now let's put in there do some documents. We can do this with PUT requests or with POST requests. With PUT, we have to specify the document ID in the URL:
In[5]:= URLExecute[HTTPRequest[
"http://127.0.0.1:5984/mydatabase/1",
<|"Method"->"PUT",
"ContentType" -> "application/json",
"Body"-> ExportString[<|"random"->RandomInteger[10,10]|>,"RawJSON"]|>]]
Out[5]= {"rev" -> "1-683f50d046d9d04090215fac932eb395", "id" -> "1", "ok" -> True}
Now lets read this back:
In[6]:= Import["http://127.0.0.1:5984/mydatabase/1","RawJSON"]["random"]
Out[6]= {0, 7, 7, 10, 2, 10, 8, 8, 9, 8}
Lets now create a document with a POST request:
In[7]:= newWithPOST = URLExecute[ HTTPRequest[
"http://127.0.0.1:5984/mydatabase/",
<|"Method" -> "POST",
"ContentType" -> "application/json",
"Body" -> ExportString[<|"random" -> RandomInteger[10, 10]|>,"RawJSON"]|>],"RawJSON"]
Out[7]= {"rev" -> "1-7c2799fa64b5dd6d9d66be7af3f8de15", "id" -> "0467a21cd602ed6827dc8fad3e0014a2", "ok" -> True}
And now we read it back:
In[8]:= Import[URLBuild[{"http://127.0.0.1:5984/mydatabase/",newWithPOST["id"]}],"RawJSON"]["random"]
Out[8]= {3,8,4,5,0,8,5,10,5,1}
We can also use other commands like URLFetch
but it doesn't seem natural to "insert" using a "fetch".
URLFetch
for post queries? That's how you do it. Some things could go wrong, for example you say that you have "write privileges" but how is the server going to know that? $\endgroup$ – C. E. Mar 21 '17 at 12:36