Tutorial
Execute on rest ninja doesn’t work with Safari because it requires HTTPS. Since configuring HTTPS requires a valid certificate and takes some time to configure, we suggest to just use Chrome or Firefox for this tutorial.
Watch Practical testing with Rest Ninja
=== Create the Inventory Collection
The example requests on this page use the inventory collection. If not already created, run the following:
==== cURL
[source,bash]
curl -i -X PUT [INSTANCE-URL]/inventory -H “Authorization: Basic [BASIC-AUTH]”
curl -i -X POST [INSTANCE-URL]/inventory?wm=upsert -H “Authorization: Basic [BASIC-AUTH]” \ -d ‘[ { “item”: “journal”, “qty”: 25, “size”: { “h”: 14, “w”: 21, “uom”: “cm” }, “status”: “A” }, { “item”: “notebook”, “qty”: 50, “size”: { “h”: 8.5, “w”: 11, “uom”: “in” }, “status”: “A” }, { “item”: “paper”, “qty”: 100, “size”: { “h”: 8.5, “w”: 11, “uom”: “in” }, “status”: “D” }, { “item”: “planner”, “qty”: 75, “size”: { “h”: 22.85, “w”: 30, “uom”: “cm” }, “status”: “D” }, { “item”: “postcard”, “qty”: 45, “size”: { “h”: 10, “w”: 15.25, “uom”: “cm” }, “status”: “A” } ]’ —-
==== HTTPie
[source,bash]
http PUT [INSTANCE-URL]/inventory Authorization:”Basic [BASIC-AUTH]”
echo ‘[ { “item”: “journal”, “qty”: 25, “size”: { “h”: 14, “w”: 21, “uom”: “cm” }, “status”: “A” }, { “item”: “notebook”, “qty”: 50, “size”: { “h”: 8.5, “w”: 11, “uom”: “in” }, “status”: “A” }, { “item”: “paper”, “qty”: 100, “size”: { “h”: 8.5, “w”: 11, “uom”: “in” }, “status”: “D” }, { “item”: “planner”, “qty”: 75, “size”: { “h”: 22.85, “w”: 30, “uom”: “cm” }, “status”: “D” }, { “item”: “postcard”, “qty”: 45, “size”: { “h”: 10, “w”: 15.25, “uom”: “cm” }, “status”: “A” } ]’ | http POST [INSTANCE-URL]/inventory wm==upsert Authorization:”Basic [BASIC-AUTH]” —-
==== JavaScript
[source,javascript]
const data = [ { “item”: “journal”, “qty”: 25, “size”: { “h”: 14, “w”: 21, “uom”: “cm” }, “status”: “A” }, { “item”: “notebook”, “qty”: 50, “size”: { “h”: 8.5, “w”: 11, “uom”: “in” }, “status”: “A” }, { “item”: “paper”, “qty”: 100, “size”: { “h”: 8.5, “w”: 11, “uom”: “in” }, “status”: “D” }, { “item”: “planner”, “qty”: 75, “size”: { “h”: 22.85, “w”: 30, “uom”: “cm” }, “status”: “D” }, { “item”: “postcard”, “qty”: 45, “size”: { “h”: 10, “w”: 15.25, “uom”: “cm” }, “status”: “A” } ];
await fetch(‘[INSTANCE-URL]/inventory’, { method: ‘PUT’, headers: { ‘Authorization’: ‘Basic [BASIC-AUTH]’ } });
await fetch(‘[INSTANCE-URL]/inventory?wm=upsert’, { method: ‘POST’, body: JSON.stringify(data), headers: { ‘Authorization’: ‘Basic [BASIC-AUTH]’ } }); —-
GET Documents from the Collection
Now let’s get all documents in a row. For this, we send a GET request to the whole collection:
GET /inventory HTTP/1.1
[
{
"_id": {
"$oid": "5d0b3dbc2ec9ff0d92ddc2aa"
},
"_etag": {
"$oid": "5d0b3dbc2ec9ff0d92ddc2a5"
},
"item": "postcard",
"qty": 45,
"size": {
"h": 10,
"w": 15.25,
"uom": "cm"
},
"status": "A"
},
{
"_id": {
"$oid": "5d0b3dbc2ec9ff0d92ddc2a9"
},
"_etag": {
"$oid": "5d0b3dbc2ec9ff0d92ddc2a5"
},
"item": "planner",
"qty": 75,
"size": {
"h": 22.85,
"w": 30,
"uom": "cm"
},
"status": "D"
},
...
]
GET filtered Documents from the Collection
It’s possible to apply a filter at the end of the request to reduce the number of output documents. The following request asks for all documents with a “qty” property greather than 75:
GET /inventory?filter={"qty":{"$gt":75}} HTTP/1.1
[
{
"_id": {
"$oid": "5d0b3dbc2ec9ff0d92ddc2a8"
},
"_etag": {
"$oid": "5d0b3dbc2ec9ff0d92ddc2a5"
},
"item": "paper",
"qty": 100,
"size": {
"h": 8.5,
"w": 11,
"uom": "in"
},
"status": "D"
}
]
Note that only the retrieved document meets the filter’s condition.
Watch How to filter data
POST a new document
Now we are going to insert a new document to the collection.
POST /inventory HTTP/1.1
{"item": "newItem", "qty": 10, "size": { "h": 2, "w": 4, "uom": "cm" }, "status": "C"}
ETag: 5d0b47422ec9ff0d92ddc2ad
X-Powered-By: restheart.org
Content-Type: application/json
Location: http://localhost:8080/inventory/5d0b47425beb2029a8d1bc72
Note the Location
header in the response, as it contains a link to the newly created document! To get the document you can directly copy that link and use it in a subsequent query.
PUT a new document
It’s possible to PUT a document into the collection by specifing the document identifier at the end of the request:
PUT /inventory/newDocument HTTP/1.1
{ "item": "yetAnotherItem", "qty": 90, "size": { "h": 3, "w": 4, "uom": "cm" }, "status": "C" }
PATCH a document
PATCH /inventory/newDocument HTTP/1.1
{ "qty": 40, "status": "A", "newProperty": "value" }
{
"_id": "newDocument",
"item": "yetAnotherItem",
"qty": 40,
"size": {
"h": 3,
"w": 4,
"uom": "cm"
},
"status": "A",
"_etag": {
"$oid": "5d0b4b9e2ec9ff0d92ddc2af"
},
"newProperty": "value"
}
The previous request changes the document created in the previous example as indicated in the request body.
DELETE a document
DELETE /inventory/newDocument HTTP/1.1