Basics - npolar/api.npolar.no GitHub Wiki

JSON API

(Pure) Core with CouchDB storage

# config.ru
map "/arctic/animal" do 
  run Npolar::Api::Core.new(nil, { :storage => Npolar::Storage::Couch.new("https://username:password@localhost:6984/arctic_animal") }) 
end

Document API

Create (PUT)

curl -i -X PUT http://localhost:9393/arctic/animal/polar-bear.json -d'{"id": "polar-bear", "species":"Ursus maritimus"}'
HTTP/1.1 201 Created
Content-Type: application/json

{
  "_id": "polar-bear",
  "_rev": "1-9c8fb39bfacc81cc5e39610f9cf81df2",
  "id": "polar-bear",
  "species": "Ursus maritimus"
}

Create (POST)

curl -i -X POST http://localhost:9393/arctic/animal/.json -d '{"species":"Odobenus rosmarus", "en": "Walrus"}'
HTTP/1.1 201 Created

{"_id":"d5fbc7e78bcb21836abf82a96c0009e9","_rev":"1-b3917c5de68075fea8c0e83311c8ad39","species":"Odobenus rosmarus","en":"Walrus"}

Retrieve (GET)

List documents

curl -i -X GET http://localhost:9393/arctic/animal/_ids.json
{ "ids": ["d5fbc7e78bcb21836abf82a96c000182", "polar-bear"] }

Get document

curl -i -X GET http://localhost:9393/arctic/animal/d5fbc7e78bcb21836abf82a96c000182.json
HTTP/1.1 200 OK

{"_id":"d5fbc7e78bcb21836abf82a96c000182","_rev":"1-b3917c5de68075fea8c0e83311c8ad39","species":"Odobenus rosmarus","en":"Walrus"}

Update (PUT)

curl -i -X PUT http://localhost:9393/arctic/animal/polar-bear.json -d'{"_id":"polar-bear","_rev":"1-9c8fb39bfacc81cc5e39610f9cf81df2","id":"polar-bear","species":"Ursus maritimus", "en": "Polar bear", "nn": "Isbjørn", "nb":"Isbjørn"}'

The above works '''once''' because the document body contains the correct revision. If you replay the PUT, you will get a HTTP 409 Conflict error, see Revisions for how to deal with this.

curl -i -X PUT http://localhost:9393/arctic/animal/polar-bear.json -d '{}'
HTTP/1.1 409 Conflict

Delete

curl -X DELETE http://localhost:9393/arctic/animal/d5fbc7e78bcb21836abf82a96c000182.json?rev=1-b3917c5de68075fea8c0e83311c8ad39

Again, attempting to delete without a revision, or with the wrong revision, leads to a HTTP 409 Conflict response.