for your convenience: command line cypher the comfortable way with httpie

Today a tweet from @rafacm drove my attention to httpie, a cURL tool for humans. Using cURL is cumbersome and noisy, httpie makes it fairly easily usable.

install httpie

Httpie is python based therefore it’s trivial to install using easy_install/pip. On my Ubuntu 13.10 box I’ve used:

sudo pip install --upgrade httpie

It fetches the package and installs it locally.

running cypher statements

Running cypher statements is much more easy than using cURL. In cURL you have to manually assemble a json snippet to pass in, deal with content types. httpie makes it easy. The following code snippets are intended to be run on a single line, for readability I’ve splitted them.

To use the old-style non-transactional cypher endpoint just use

http -b -j localhost:7474/db/data/cypher
 query="START n=node(*) return n limit 2"

for non-parameterized queries and

http -b -j localhost:7474/db/data/cypher 
  query="MATCH (m:Movie {title:{title}}) return m" 
  params:='{"title":"The Matrix"}'

for parameterized queries. Httpie sets the ‘Accept’ header to json if option -j is used. In case  of -j httpie assembles the key-value request items internally into json format as well. Since the params contains a json map it needs to be assigned with “:=” instead of just “=”.

For the transactional Cypher endpoint in Neo4j 2.0 httpie can be used like this:

http -b -j localhost:7474/db/data/transaction/commit 
 statements:='[{"statement": "MATCH (m:Movie {title:{title}}) return m", 
  "parameters": {"title":"The Matrix"} }]'