Skip to content

Latest commit

 

History

History
117 lines (81 loc) · 3.3 KB

File metadata and controls

117 lines (81 loc) · 3.3 KB

CouchDB REST API

Introduction

In this exercise you have to intall CouchDB and interact with it using CouchDB's RESTful Web API.

Preparation

  • Install CouchDB on your computer.
  • Visit Fauxton (=CouchDB's web-based administration tool) at http://localhost:5984/_utils/#setup and choose Configure a Single Node
    • Enter username and password
    • Use 127.0.0.1 as the IP address (=CouchDB not available on the network, just on your computer)
    • Accept the suggested port (e.g. 5984)

Requirements

  • Install CouchDB as described above.
  • Use any REST client (e.g. Postman, Visual Studio Code) to execute the operations below

Exercises

Check if CouchDB works

GET /

GET http://127.0.0.1:5984/ HTTP/1.1

Create a Database

PUT /db

PUT http://127.0.0.1:5984/my_first_db HTTP/1.1
Accept: application/json
Authorization: Basic YWRtaW46YWRtaW4=

Query Database Properties

GET /db

GET http://127.0.0.1:5984/my_first_db HTTP/1.1
Accept: application/json
Authorization: Basic YWRtaW46YWRtaW4=

Add a Document

POST /db

POST http://127.0.0.1:5984/my_first_db HTTP/1.1
Accept: application/json
Content-Type: application/json
Authorization: Basic YWRtaW46YWRtaW4=

{
  "schoolName": "HTL Perg", 
  "location": "Perg", 
  "country": "Austria" 
}

Find a Document

POST /db/_find

POST http://127.0.0.1:5984/my_first_db/_find HTTP/1.1
Accept: application/json
Content-Type: application/json
Authorization: Basic YWRtaW46YWRtaW4=

{
  "selector": { 
    "country": { "$eq": "Austria" } 
  }, 
  "fields": [ "_id", "_rev", "schoolName" ] 
}

Delete a Document

DELETE /db/_id?rev=_rev

DELETE http://127.0.0.1:5984/my_first_db/6fd2474efa515a3a84fe2e0496000387?rev=1-c03973c2778ca77eef6b2f64a069df03 HTTP/1.1
Accept: application/json
Authorization: Basic YWRtaW46YWRtaW4=

Delete Database

DELETE /db

DELETE http://127.0.0.1:5984/my_first_db HTTP/1.1
Accept: application/json
Authorization: Basic YWRtaW46YWRtaW4=

Hints

  • To get the Authorization header, you have to Base64-encode username:password

Advanced Exercises

  • Read details about CouchDB's REST API
  • Build a web UI (HTML, JavaScript, fetch API) for manipulating documents in CouchDB