User Tools

Site Tools


couchdb

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
couchdb [2014/12/28 12:16]
sebastian Fixed headlines
couchdb [2015/05/17 17:44] (current)
Line 28: Line 28:
  
 ===== Adding and updating documents ===== ===== Adding and updating documents =====
-Now we are going to create the first entry in the database by using HTTP ''​PUT'':​+Now we are going to create the first entry in the database by using HTTP ''​POST'':​
 <​code>​ <​code>​
-curl -X PUT http://​localhost:​5984/​inventory/​mft_3-zk ​-d '​{"​type":"​part","​article_name":"​MFT/​3-ZK","​friendly_name":"​Zusatzklemmung","​amount":"​1","​part_number":"​495541","​ean":"​4014549099421"​}'​+curl -X POST http://​localhost:​5984/​inventory/​ -d '​{"​type":"​part","​article_name":"​MFT/​3-ZK","​friendly_name":"​Zusatzklemmung","​amount":"​1","​part_number":"​495541","​ean":"​4014549099421","​manufacturer":"​Festool"}' -H '​Content-Type:​application/​json'
 </​code>​ </​code>​
 +We could have used HTTP ''​PUT''​ instead of ''​POST''​ - which is in fact recommended,​ because PUT is safer in case the request is repeated. But ''​POST''​ automatically generates a unique document ID. The document ID is the key that identifies the document inside of your CouchDB database. You will notice it in the URLs later.
 +
 +So now we are going to repeat the same command with ''​PUT''​ instead of ''​POST''​. Later we are going to delete the duplicate entry.
 +<code bash>
 +curl -X PUT http://​localhost:​5984/​inventory/​festool.mft_3_zk -d '​{"​type":"​part","​article_name":"​MFT/​3-ZK","​friendly_name":"​Zusatzklemmung","​amount":"​1","​part_number":"​495541","​ean":"​4014549099421","​manufacturer":"​Festool"​}'​
 +</​code>​
 +
  
 CouchDB answers with a JSON document that contains the revision ID of the document you just stored. Write it down, we are going to need it in a second. CouchDB answers with a JSON document that contains the revision ID of the document you just stored. Write it down, we are going to need it in a second.
Line 37: Line 44:
 Oops, we forgot to add the manufacturer. No problem though, we can just write a new revision of the document using the same URL. Oops, we forgot to add the manufacturer. No problem though, we can just write a new revision of the document using the same URL.
 <​code>​ <​code>​
-curl -X PUT http://​localhost:​5984/​inventory/​mft_3-zk ​-d '​{"​_rev":"​revision-id","​type":"​part","​article_name":"​MFT/​3-ZK","​friendly_name":"​Zusatzklemmung","​amount":"​1","​part_number":"​495541","​ean":"​4014549099421","​manufacturer":"​Festool"​}'​+curl -X PUT http://​localhost:​5984/​inventory/​festool.mft_3_zk ​-d '​{"​_rev":"​revision-id","​type":"​part","​article_name":"​MFT/​3-ZK","​friendly_name":"​Zusatzklemmung","​amount":"​1","​part_number":"​495541","​ean":"​4014549099421","​manufacturer":"​Festool"​}'​
 </​code>​ </​code>​
 Make sure to replace the ''​revision-id''​ placeholder with the ID you wrote down earlier. Make sure to replace the ''​revision-id''​ placeholder with the ID you wrote down earlier.
Line 44: Line 51:
 How do we read the entry? You probably already guessed it: How do we read the entry? You probably already guessed it:
 <​code>​ <​code>​
-curl -X GET http://​localhost:​5984/​inventory/​mft_3-zk+curl -X GET http://​localhost:​5984/​inventory/​festool.mft_3_zk
 </​code>​ </​code>​
 You can drop the ''​-X GET''​ part since this is curl's default behavior. It has been added here for demonstration purposes. You can drop the ''​-X GET''​ part since this is curl's default behavior. It has been added here for demonstration purposes.
  
 +===== Using the built in web GUI: Futon =====
 +While using curl is perfect to get a feeling for the HTTP API, at some point the typing effort gets cumbersome.
 +
 +Introducing:​ Futon. Just open the following URL in your browser an you get a simple web frontend for managing your database and documents.
 +<​code>​
 +http://​localhost:​5984/​_utils/​
 +</​code>​
couchdb.txt ยท Last modified: 2015/05/17 17:44 (external edit)