layout | title | permalink |
---|---|---|
page-notitle |
Try |
/try.html |
Use curl or httpie to play with our demo instance.
See below for a step by step tutorial.
RESTHeart uses the Hypertext Application Language json representation format and embeds the HAL browser.
Go and play with it on our demo instance at dbapi.io
.
The demo database exposes a collection at http://dbapi.io/db/coll
without requiring authentication. Access to any other MongoDB resource is forbidden.
This demo instance is reset on regular basis, so feel free to play with it but don't rely on it for persistent storage of your data. {: .bs-callout .bs-callout-info }
Insert or update the document with _id docid
PUT /db/coll/docid
$ curl -i -H "Content-Type: application/json" -X PUT http://dbapi.io/db/coll/docid -d '{"from":"ujibang", "message":"RESTHeart rocks!!" }'
HTTP/1.1 201 Created ...
{% endhighlight %}
</div>
Get the created document
$ curl http://dbapi.io/db/coll/docid
{ "_id": "docid", "from": "ujibang", "message": "RESTHeart rocks", "_etag": { "$oid": "57069cb9c9e77c00078dc780" } }
{% endhighlight %}
</div>
Create a second document with POST /db/coll
If the _id is not specified in the request body, it will be autogenerated as a new ObjectId. The Location
response header specifies the URL of the new document.
$ curl -i -H "Content-Type: application/json" -X POST http://dbapi.io/db/coll -d '{"from":"ujibang", "message": "MongoDB rocks as well!"}'
HTTP/1.1 201 Created Location: http://dbapi.io/db/coll/563a40d6e4b0ef984cae182b ...
{% endhighlight %}
</div>
Update the first document with PATCH /db/coll/docid
This request uses the dot notation and the $currentDate
operator. These are available in all write requests!
$ curl -i -H "Content-Type: application/json" -X PATCH http://dbapi.io/db/coll/docid -d '{"$currentDate": { "header.timestamp": true}}'
HTTP/1.1 200 OK ...
{% endhighlight %}
</div>
Get the updated document again.
The returned representation contains all the document properties plus few more. The _etag
is updated automatically by RESTHeart for Web caching and ghost writes management.
$ curl http://dbapi.io/db/coll/docid
{ "_id": "docid", "from": "ujibang", "message": "RESTHeart rocks!!", "header": { "timestamp": { "$date": 1475598488601 } }, "_etag": { "$oid": "5718d948c9e77c000609f677" } }
{% endhighlight %}
</div>
Find documents via query.
The filter
query parameter allows to specify any MongoDB query.
The matching documents are in the _embedded
array.
$ curl http://dbapi.io/db/coll?filter='\{"from":"ujibang"\}'
{ "_id": "coll", "_returned": 2, "_embedded": [{ "_id": "docid", "from": "ujibang", "message": "RESTHeart rocks!", "header": { "timestamp": { "$date": 1475598488601 } }, "_etag": { "$oid": "57069cb9c9e77c00078dc780" } }, { "_id": { "$oid": "563a40d6e4b0ef984cae182b" }, "from": "ujibang", "message": "MongoDB rocks as well!", "_etag": { "$oid": "57175e2dc9e77c0006eb9ef4" } }], "_etag": { "$oid": "57f3d7d0c9e77c00075daae9" } }
{% endhighlight %}
</div>
<div class="row" style="margin-top: 20px">
<div class="col-md-3" style="padding-top:7px">
<p><strong>Get</strong> documents as an array (without the collection properties).</p>
<p>The <code>np</code> query parameter allows to get rid of the collection properties.</p>
<p>In this case, the response body is just an array of documents.</p>
<p><code>np</code> stands for No Properties</p>
</div>
<div class="col-md-9">
{% highlight bash %}
$ curl http://dbapi.io/db/coll?np
[{ "_id": "docid", "from": "ujibang", "message": "RESTHeart rocks!", "header": { "timestamp": { "$date": 1475598488601 } }, "_etag": { "$oid": "57069cb9c9e77c00078dc780" } }, { "_id": { "$oid": "563a40d6e4b0ef984cae182b" }, "from": "ujibang", "message": "MongoDB rocks as well!", "_etag": { "$oid": "57175e2dc9e77c0006eb9ef4" } }]
{% endhighlight %}
</div>